# Programming Ruby book p.50 (Fibonacci yield example)

**URL:** <https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544>\
**Category:** Ruby\
**Created:** [March 3, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544 "2007-03-03T18:13:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [March 3, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544/1 "2007-03-03T18:13:18Z")

</div>

here’s the example:

1. def fib\_up\_to(max)
2. i1,i2 = 1,1
3. while i1 \<= max
4. 

```
yield i1

```

5. 

```
i1, i2=i2, i1+i2

```

6. end
7. end
8. 
9. fib\_up\_to(1000) {|f| print f, " "}

I’m familiar with yield passing values to the block and vice-versa. But  
I have no clue what is happenening in line 5. Or at least in general  
can anyone explain what is going on here in these 9 lines of code?  
(I’ve been programming for 10 years so I’m familiar methods, loops, etc.  
it’s just this yield thing I don’t completely understand especially line  
5.).

cheers,  
mike

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [March 3, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544/2 "2007-03-03T18:26:27Z")

</div>

Mike G. wrote:

> 1. fib\_up\_to(1000) {|f| print f, " "}

Hi Mike,

What’s happening at line 5 is a swap. The code is equivalent to this:

tmp = i1+i2  
i1 = i2  
i2 = tmp

## Hope that helps.

Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [March 3, 2007, 6:34pm UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544/3 "2007-03-03T18:34:28Z")

</div>

On Mar 3, 2007, at 12:13 PM, Mike G. wrote:

> 1. 
> 
> ```
> i1, i2=i2, i1+i2
> 
> ```

[…]

> But I have no clue what is happenening in line 5.

The spacing in your text is a bit misleading.  
This makes it a little clearer:

```
   i1,i2 = i2,(i1+i2)

```

This is Ruby’s multiple assignment statement. The  
expressions on the right are evaluated into a list and  
assigned, in order, to the variables on the left.

Gary W.

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [March 3, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544/4 "2007-03-03T18:31:27Z")

</div>

Mike G. wrote:

> I’m familiar with yield passing values to the block and vice-versa. But  
> I have no clue what is happenening in line 5.

Line 5 has nothing to do with the yield (which in this case just  
displays the  
current value of i1). It assigns the value of i2 to i1 and the sum of  
the  
values of i1 (before the assignment) and i2 to i2.

> (I’ve been programming for 10 years so I’m familiar methods, loops, etc.  
> it’s just this yield thing I don’t completely understand especially line  
> 5.).

Like I said: The yield (in line 4) only displays the value and line 5  
has  
nothing to do with yield.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [March 3, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544/5 "2007-03-03T18:57:30Z")

</div>

Ok thanx guys. I just found that multiple assignment is not explained  
until p.91 of the text while this example is on page 50 … strange.,

> mike

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [March 4, 2007, 6:21am UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544/6 "2007-03-04T06:21:28Z")

</div>

On Sun, Mar 04, 2007 at 02:34:00AM +0900, Gary W. wrote:

> This is Ruby’s multiple assignment statement. The  
> expressions on the right are evaluated into a list and  
> assigned, in order, to the variables on the left.

I probably would have done it thusly:

i1, i2 = i2, i1 + i2  
. . . or:  
i1, i2 = [i2, i1 + i2]

. . . or something to that effect. Somehow, it seems more readable to  
me. YMMV.

Do my versions for any particular reason seem unlike the typical Ruby  
idiom for some reason? I’m curious.

---

<div class="post-metadata">

**Author:** ![random52k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@random52k](https://www.ruby-forum.com/u/random52k)\
**Post date:** [March 4, 2007, 7:55am UTC](https://www.ruby-forum.com/t/programming-ruby-book-p-50-fibonacci-yield-example/87544/7 "2007-03-04T07:55:33Z")

</div>

I happen to have this book as well. There’s actually a comment  
immediate to the right of the line i1, i2 = 1, 1 and says # parallel  
assignment (i1 = 1 and i2 =1)

i1, i2 = i2, i1+i2  
is another way of writing  
temp = i1  
i1 = i2  
i2 = temp+i2
