Ruby constants are a nice place to put application configuration information, but they can be inflexible if you want to defer initialization until later — for example, if you want a constant to have a given value at application startup only if a certain environment variable or command–line parameter is set. I like the idea of the single–assignment variables that you get in many functional and logic languages, and I also like the idea that constants only really need to be constant after they've been read once. (See for some interesting implications of that idea in Java programs.)
The is a little hack I put together to make it easy to have write-once, defaultable constants in your Ruby programs. A quiescing constant, or quiescent, has an optional default value (either an explicit value or a block to calculate that value), and can be assigned to at most once in a program execution. Once the quiescent is written to, its value quiesces and it becomes a normal constant. If it is read without being explicitly quiesced, it assumes the default value (or the result of executing the default-value block) and becomes a normal constant. Here's an example to make things clearer:
Get quiescent from or from .
Leave a comment