My understanding is that the if
statements at the end of the line are evaluated before the code at the front of the line:
'never shown' if (false)
And assignment is possible in an if
statement.
'shown' if (value = 'dave is king')
value #=> "dave is king"
And, when a variable that doesn't exist is assigned to, it is created. There is no need for it to exist beforehand. Is this true?
If all these assumptions are true, why does this fail?
error_array << error if (error = import_value(value))
#=> undefined local variable or method `error' for
It assigned to error before the array push right? I want to understand when things are evaluated.
This one does work:
if (error = import_value(value))
error_array << error
end
Now I'm really confused.
error_array << error if( error = import_value(value) )
the if statement equates to true here so you're trying to appenderror
toerror_array
, buterror
is nil – Saddlebackederror
to already exist. but it's really moot because you should avoid stupid code-compression tricks and just write the block. vertical space is not a scarce resource. – Terzaslast if ...
and other control flow, where the action is both dead simple and more important than the condition, but the given example is absurd. – Terzasif error = import_value(value) then error_array << error end
? – Overshadow