# Rails Book Source code Question

**URL:** <https://www.ruby-forum.com/t/rails-book-source-code-question/52874>\
**Category:** Rails\
**Created:** [November 22, 2005, 9:01pm UTC](https://www.ruby-forum.com/t/rails-book-source-code-question/52874 "2005-11-22T21:01:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [November 22, 2005, 9:01pm UTC](https://www.ruby-forum.com/t/rails-book-source-code-question/52874/1 "2005-11-22T21:01:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [November 22, 2005, 9:12pm UTC](https://www.ruby-forum.com/t/rails-book-source-code-question/52874/2 "2005-11-22T21:12:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [November 22, 2005, 9:17pm UTC](https://www.ruby-forum.com/t/rails-book-source-code-question/52874/3 "2005-11-22T21:17:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [November 23, 2005, 1:32pm UTC](https://www.ruby-forum.com/t/rails-book-source-code-question/52874/4 "2005-11-23T13:32:25Z")

</div>

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 S.
