I'm not talking about bad breath. Rather, I'm talking about scope in ruby.
First off, I'm not talking about bad breath. Rather, I'm talking about scope in ruby.
The previous entry, attributes with default values (on steroids), had an issue with it, due to the way in which ruby defines scope (as of 1.8, scope is supposed to change in 1.9). Here's the "broken" code:
The problem is with the value variable. Because it is set within the Object#attribute method, the reference to value is the same throughout the entire method. So, when we change value, by setting it in the "setter" method, we are changing it for every instance of the variable. In addition, since we're setting it at the class level, any object which belongs to the class for which we are setting it has the same value. The solution is pretty simple: