Ruby's "foo = true if !defined? foo || foo.nil?" won't work -


i thought in following, foo should true

$ irb  ruby-1.9.2-p0 > foo = true if !defined? foo || foo.nil?  => nil   ruby-1.9.2-p0 > foo  => nil  

because foo @ first not defined, foo = true part make temporarily has nil value, !defined didn't catch it, foo.nil? should catch it, , make true... why still nil?

this related ruby's "foo = true if !defined? foo" won't work expected

be careful when skipping parenthesis. meant:

foo = true if !defined?(foo) || foo.nil? 

as per other question, defined?(foo) true, want write:

foo = true if foo.nil? 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -