Re:Ruby statement modifiers behave differently than conditio...
by chief on Monday December 28, 2009 @01:50PM
(#1019102)
I think the article is wrong. His rewrite on line 06. should use if instead of unless.
$ irb
>> b
NameError: undefined local variable or method `b' for main:Object
from (irb):1
>> if defined? b
>> b = 5
>> end
=> nil
>> b
=> nil
>> defined? b
=> "local-variable"
Re:Ruby statement modifiers behave differently than conditio...
by angilly on Monday December 28, 2009 @02:05PM
(#1019126)
No, it's using unless like I intended. But your example also demonstrates the weird behavior. As a few people have pointed out here and over in the post comments, the issue at hand isn't so much about statement modifiers as it is about how Ruby 'defines' local variables.
Take this for example:
ra:~$ irb
a = irb(main):001:0> a = b
NameError: undefined local variable or method `b' for main:Object
from (irb):1
irb(main):002:0> defined? a
=> "local-variable"
irb(main):003:0>
It just so happens that using statement modifiers in the way I tried using them brings this behavior to light.
I've just internalized using the longer form when using assignment in a conditional.