Going through the Rails book samples, on page 68 they have some code
that goes like this:
<%
odd_or_even = 0
for product in @products
odd_or_even = 1 - odd_or_even
%>
The code works fine for me, but I cannot figure out how the line:
odd_or_even = 1 - odd_or_even
is working? I mean how does something initialized to zero, then set to
1, then minus one, ever go beyond the value zero? I just don’t under
stand how this is working in Ruby. It would seem like normally it would
be written like:
if odd_or_even = 1
odd_or_even = 0
else
odd_or_even = 1
end
Obviously the way in the book is a shortcut which I have no problem
with, it just doesn’t make sense to me.
Thanks.