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. -- Posted via http://www.ruby-forum.com/.
dteare wrote:> If odd_or_even is 0, then odd_or_even = 1 - odd_or_even is 1. > > If odd_or_even is 1, then odd_or_even = 1 - odd_or_even is zero. > > Repeat.Thanks, that actually cleared it up 100% for me. Until it was laid out like that I just didn''t see it working like it was. -Mark -- Posted via http://www.ruby-forum.com/.
If odd_or_even is 0, then odd_or_even = 1 - odd_or_even is 1. If odd_or_even is 1, then odd_or_even = 1 - odd_or_even is zero. Repeat. On 22-Nov-05, at 3:01 PM, Mark Haliday wrote:> 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
1 - 1 == 0 # 1 ==> 0 1 - 0 == 1 # 0 ==> 1 It flops back and forth from 1 to 0 and back to 1 and then back to 0... Warren Seltzer -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Mark Haliday Sent: Tuesday, November 22, 2005 10:01 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] Rails Book Source code Question 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?