|
|
|
|
|
by weaksauce
1161 days ago
|
|
there are many ways to lookup if something is defined or not instance_variable_defined?
defined?
are two common onesbut if you are using something before defining it you are going to crash so that's definitely one way of distinguishing it. using fetch is another way to provide a default value to something that may have a nil value as a meaningful value. |
|
> but if you are using something before defining it you are going to crash so that's definitely one way of distinguishing it.
Would you clarify? Like that was my point, casually using something where `nil` is a possible assigned value, or maybe the variable hasn't been defined, makes possible the (sadly common) category of bugs where the program does not crash, but proceeds as though the variable was assigned `nil`, but actually the variable was never defined.
In contrast to local variables where the program will crash if you reference the variable but it hasn't been defined.
My comment meant "bare instance variables in Ruby are not great [and we might not want to recommend them as a solution to people complaining about Ruby/Rails quirks]". Do you disagree and actually love the behavior of Ruby instance variables? Or are you simply technically correcting me? (which, again, I appreciate)