Wednesday, 14 August 2013

Position of "if" condition

Position of "if" condition

Since I'm doing Ruby (on Rails), I always thought that :
do_something if condition
were equivalent to
if condition
do_something
end
For the first time, I found a code that does not respect this rule...
if (defined? foo)
foo = default_value
end
Here, foo takes the default value.
foo = default_value if !(defined? foo)
Here, foo takes nil.
I thing that, in the previous line, the if is exectuted first ; it is
equivalent to
foo = (default_value if !(defined? foo))
Is there any "beautiful" writing to set in a view a default value if the
variable is not defined ? I instist on the "not defined", witch is not
equal as "nil".
Thanks

No comments:

Post a Comment