Although this is generally true, it can bite you as this is not exactly the case.
For example, consider the following:
>>
>> a=5 if true
>> end
=> nil
>> test1
=> 5
That looks ok, and the return value of 5 shouldn't surprise you as that was the last expression evaluated. Now how about this (below)? What is the last expression evaluated here?>>
>> a=5 if false
>> end
=> nil
>> test2
=> nilDid you expect that result? I didn't. I would say 'false' is the last expression evaluated, so I would expect the return value to be false. But instead of false, I get nil.Why? It would seem that expressions evaluated as the controlling expression of a conditional statement (if, unless, etc.) don't count as an implied return value.
Let's simplify this even more:
>>
>> if 2 > 3
>> end
>> end
=> nil
>> test3
=> nilSo it would appear it doesn't matter. And again...>>
>> if 3 > 2
>> end
>> end
=> nil
>> test4
=> nilYep, ruby doesn't care about conditional statement controlling expressions with respect to return values.Happy ruby'ing!


0 comments:
Post a Comment