On Wed, Feb 1, 2012 at 5:49 AM, Gavin S. [email protected]
wrote:
return an array of size two, making the conditional necessarily true.
if (a, b = foo)
puts “outcome 1”
else
puts “outcome 2” # I was expecting this.
end
Of course I don’t have a function that returns nil unconditionally!
Just showing the structure.
Sorry, but I find this incredibly opaque.
Could you just write what you expect the evaluation to be in each of
these
cases?
a, b = 1, 1
a, b = 1, nil
a, b = nil, 1
a, b = nil, nil
I’d expect return values of [1, 1], [1, nil], [nil, 1], [nil, nil] which
are all true.
Now does this change when the RHS is an array instead of two values?
For example, would a, b = [1, 1]
return a different value than a, b = [1, 1]
?
Your example seems particularly devious in that it only has one value on
the RHS of the assignment. This means that the second argument will be
assigned nil (because there is no value for it), and because assignment
operators always return the RHS, it will then return whatever single
value
was over there. This causes it to look like you are assigning multiple
values to the variables and having it return nil, when in fact you are
only
assigning one value (the second variable is along for the ride and will
always be nil).
There are so many nuanced ruby edge cases one must know (and then must
also
know the implementation of how it is being returned) to understand that
example that I think it has more overhead than it is worth.