Hacker News new | ask | show | jobs
by chief 6011 days ago
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"
1 comments

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.