Possible Duplicate:
conditional statement and assigning value in ruby
While refactoring some rails code, I have encountered some syntax oddity in ruby.
Given the following method
def get_value
42
end
Why does this work?
if value = get_value
puts value
end
While this does not?
puts value if value = get_value
The latter gives an error: undefined local variable or method `value' for main:Object (NameError)
. I thought these expressions were equal? When the if-block is evaluated before the puts, value
should not be undefined.