Write-once, defaultable constants in Ruby

| No Comments

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 this paper for some interesting implications of that idea in Java programs.)

The quiescent gem 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 GitHub or from RubyGems.

Leave a comment

About this Entry

This page contains a single entry by Will Benton published on September 2, 2011 4:17 PM.

Gliss 0.2.0 release and a gliss example was the previous entry in this blog.

Wallaby paper at SC11 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Powered by Movable Type 4.25
Секс шоп в онлайне. | cool free sex search milf I was very pleased | We offer to check wonderful wholesale fashion jewelry at this website!