Why aren't these two statements equivalent?
defined? foo ? foo << "bar" : foo = ["bar"]
if (defined? foo) then foo << "bar" else foo = ["bar"] end
First statement:
irb(main):001:0> defined? foo ? foo << "bar" : foo = ["bar"]
=> nil
irb(main):002:0> foo
=> nil
irb(main):003:0> defined? foo ? foo << "bar" : foo = ["bar"]
=> "expression"
irb(main):004:0> foo
=> ["bar"]
Second statement:
irb(main):001:0> if (defined? foo) then foo << "bar" else foo = ["bar"] end
=> ["bar"]
irb(main):002:0> foo
=> ["bar"]
irb(main):003:0> if (defined? foo) then foo << "bar" else foo = ["bar"] end
=> ["bar", "bar"]
irb(main):004:0> foo
=> ["bar", "bar"]
These sessions are with JRuby 1.5.0 (should be equivalent to native Ruby 1.8.7). I see slightly different behavior with native ruby 1.9.1: statement #1 never defines foo
even when running it twice.