# Code problems

**URL:** <https://www.ruby-forum.com/t/code-problems/105868>\
**Category:** Ruby\
**Created:** [June 4, 2007, 5:37am UTC](https://www.ruby-forum.com/t/code-problems/105868 "2007-06-04T05:37:32Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 5:37am UTC](https://www.ruby-forum.com/t/code-problems/105868/1 "2007-06-04T05:37:32Z")

</div>

why is the following code not valid

def fibber  
a = 1  
b = 1  
lambda do  
yield(a)  
a,b = a+b,a  
end  
end

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 6:06am UTC](https://www.ruby-forum.com/t/code-problems/105868/2 "2007-06-04T06:06:34Z")

</div>

Hi,

At Mon, 4 Jun 2007 12:36:50 +0900,  
david karapetyan wrote in [ruby-talk:254217]:

> why is the following code not valid

It is valid.

> def fibber  
> a = 1  
> b = 1  
> lambda do  
> yield(a)  
> a,b = a+b,a  
> end  
> end

irb(main):009:0\> fib = fibber{|x|p x}  
=\> #Proc:0xb7c63a74@:4(irb)  
irb(main):010:0\> fib[]  
1  
=\> [2, 1]  
irb(main):011:0\> fib[]  
2  
=\> [3, 2]  
irb(main):012:0\> fib[]  
3  
=\> [5, 3]

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 6:26am UTC](https://www.ruby-forum.com/t/code-problems/105868/3 "2007-06-04T06:26:36Z")

</div>

thanks. i didn’t realize i had to provide the block as well when i was  
creating the closure. i was hoping for a closure that could take a  
different  
block every time. i wanted fib to be a function that had access to a and  
b  
and every call to fib would require a block. as it is fib has its block  
fixed at creation time. do you know of a way of doing what i wanted to  
do  
originally?

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 7:17am UTC](https://www.ruby-forum.com/t/code-problems/105868/4 "2007-06-04T07:17:39Z")

</div>

Hi,

At Mon, 4 Jun 2007 13:25:50 +0900,  
david karapetyan wrote in [ruby-talk:254219]:

> thanks. i didn’t realize i had to provide the block as well when i was  
> creating the closure. i was hoping for a closure that could take a different  
> block every time. i wanted fib to be a function that had access to a and b  
> and every call to fib would require a block. as it is fib has its block  
> fixed at creation time. do you know of a way of doing what i wanted to do  
> originally?

You need ruby 1.9. Block passing to a block isn’t supported in  
1.8.

def fibber  
a = 1  
b = 1  
lambda do |&block|  
block.call(a)  
a,b = a+b,a  
end  
end

irb(main):010:0\> fib = fibber  
=\> #Proc:b7ba2338@:6(irb)  
irb(main):011:0\> 10.times {fib.call {|x|p x}}  
1  
2  
3  
5  
8  
13  
21  
34  
55  
89  
=\> 10

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/code-problems/105868/5 "2007-06-04T19:14:13Z")

</div>

On 6/3/07, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> 21  
> 34  
> 55  
> 89  
> =\> 10
> 
> –  
> Nobu Nakada

I thought I read somewhere that 1.9 wasn’t going to support closures.  
So it apparently does then?

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/code-problems/105868/6 "2007-06-04T19:20:14Z")

</div>

fREW wrote:

> I thought I read somewhere that 1.9 wasn’t going to support closures.

You are thinking of continuations, not closures, maybe?

IIRC, recent versions of YARV do not have closures.

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/code-problems/105868/7 "2007-06-04T20:00:38Z")

</div>

cool and thanks for the help

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/code-problems/105868/8 "2007-06-04T19:59:38Z")

</div>

Joel VanderWerf wrote:

> fREW wrote:
> 
> > I thought I read somewhere that 1.9 wasn’t going to support closures.
> 
> You are thinking of continuations, not closures, maybe?
> 
> IIRC, recent versions of YARV do not have closures.

Now I’m the one spreading confusion. To clarify:

YARV _does_ have closures

YARV does _not_ have continuations (at least in recent versions)

Sorry…

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 5, 2007, 1:32am UTC](https://www.ruby-forum.com/t/code-problems/105868/9 "2007-06-05T01:32:19Z")

</div>

Hi,

At Tue, 5 Jun 2007 02:58:20 +0900,  
Joel VanderWerf wrote in [ruby-talk:254306]:

> Now I’m the one spreading confusion. To clarify:
> 
> YARV _does_ have closures
> 
> YARV does _not_ have continuations (at least in recent versions)

YARV has continuations in the latest versions.

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 5, 2007, 7:26am UTC](https://www.ruby-forum.com/t/code-problems/105868/10 "2007-06-05T07:26:20Z")

</div>

On 6/4/07, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> YARV has continuations in the latest versions.  
> Wow, congratulations, that must have been tough work!

Robert

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 4, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/code-problems/105868/11 "2007-06-04T21:26:10Z")

</div>

On 6/4/07, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> YARV _does_ have closures
> 
> YARV does _not_ have continuations (at least in recent versions)
> 
> Sorry…
> 
> –  
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Isn’t YARV supposed to be Ruby 1.9? Also, can someone explain the  
difference between closures and continuations? I thought they were  
the same thing.

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 6, 2007, 5:46am UTC](https://www.ruby-forum.com/t/code-problems/105868/12 "2007-06-06T05:46:58Z")

</div>

On 6/4/07, fREW [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Also, can someone explain the  
> difference between closures and continuations? I thought they were  
> the same thing.

[http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-continuations](http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-continuations)

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 6, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/code-problems/105868/13 "2007-06-06T18:45:07Z")

</div>

On 6/5/07, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

When I first learned about closures I was like, “Weird…” But then I  
found a use for them and they are awesome. Continuations are even  
weirder. I like the groundhog day analogy. Imagine how the program  
feels!

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 6, 2007, 10:32am UTC](https://www.ruby-forum.com/t/code-problems/105868/14 "2007-06-06T10:32:26Z")

</div>

On 6/6/07, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Hi Rick I am still struggeling with continuations :(, as a matter of  
fact I hoped that they would behave as described above, but they do  
not, it seems.

When I execute the code from your blog  
def continuation  
i = 0  
lola = nil  
callcc {|cc| puts “In callcc”;lola = cc}  
puts “i is now #{i}”  
i += 1  
lola  
end  
cont = continuation

# point of looping

cont.call

[http://rubyquiz.com/quiz70.html](http://rubyquiz.com/quiz70.html)  
I just get an endless loop, do you have an explanation for it?

I remember there was a Ruby Q. [Ruby Quiz - Constraint Processing (#70)](http://rubyquiz.com/quiz70.html) with  
an excellent solution of Jim W…  
One day I thought I understood the mechanism and optimized one callcc  
away, boy I made a complete fool out of myself, you can find it on the  
archives, I am not going to give the link ;).

Well, just confused…

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 6, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/code-problems/105868/15 "2007-06-06T22:39:47Z")

</div>

On 6/6/07, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> callcc {|cc| puts “In callcc”;lola = cc}  
> puts “i is now #{i}”  
> i += 1  
> lola  
> end  
> cont = continuation
> 
> # point of looping
> 
> cont.call
> 
> I just get an endless loop, do you have an explanation for it?

Robert, you are right, it loops in ruby, but not in irb.

I was trying to cook up a simple example and got hoist by irb’s petard.

What happens in ruby is that calling the continuation goes back to the  
method, which returns to the point before the cont.call and away we go  
again.

Continuations normally aren’t reused in the way I did in the example.  
I’m actually not sure why it works the way it does in irb.

Here’s a simpler example which shows the flow of control concept.

rick@frodo:/public/rubyscripts$ cat testcontin.rb  
def continuation  
puts “Before callcc”  
callcc {|cc| puts “In callcc”;return cc}  
puts “After callcc”  
end

puts “Begin”  
cont = continuation  
puts “Got cont #{cont.inspect}”  
if cont  
cont.call  
puts “After call”  
end  
puts “All done”  
rick@frodo:/public/rubyscripts$ ruby testcontin.rb  
Begin  
Before callcc  
In callcc  
Got cont #Continuation:0xb7de3840  
After callcc  
Got cont nil  
All done

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 6, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/code-problems/105868/16 "2007-06-06T23:03:50Z")

</div>

On 6/6/07, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Robert, you are right, it loops in ruby, but not in irb.
> 
> I was trying to cook up a simple example and got hoist by irb’s petard.
> 
> What happens in ruby is that calling the continuation goes back to the  
> method, which returns to the point before the cont.call and away we go  
> again.  
> That confirms my limited understanding of it 🙂
> 
> Continuations normally aren’t reused in the way I did in the example.  
> I’m actually not sure why it works the way it does in irb.  
> Hmm maybe I can give some hint in my zenwalk ruby 1.8.6 irb it loops  
> as well as in my Windows (one click installer) irb, do you use a  
> special irb setup?  
> puts “Begin”  
> In callcc  
> Got cont #Continuation:0xb7de3840  
> After callcc  
> Got cont nil  
> All done

That is a good example I really like to build on it.  
Thanks.

Maybe I might add one thing which took me some minutes to work out 😉  
the nil value for the cont variable comes from : puts “After call”.

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 7, 2007, 1:15am UTC](https://www.ruby-forum.com/t/code-problems/105868/17 "2007-06-07T01:15:10Z")

</div>

On 6/6/07, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That is a good example I really like to build on it.  
> Thanks.
> 
> Maybe I might add one thing which took me some minutes to work out 😉  
> the nil value for the cont variable comes from : puts “After call”.

Actually it comes from the puts “After callcc” as this change  
demonstrates;

rick@frodo:/public/rubyscripts$ cat testcontin.rb  
def continuation  
puts “Before callcc”  
callcc {|lola| puts “In callcc”;return lola}  
puts “After callcc”  
“returning”  
end

puts “Begin”  
cont = continuation  
puts “Got cont #{cont.inspect}”  
if cont  
cont.call  
puts “After call”  
“Hello”  
end  
puts “All done”  
rick@frodo:/public/rubyscripts$ ruby testcontin.rb  
Begin  
Before callcc  
In callcc  
Got cont #Continuation:0xb7d58768  
After callcc  
Got cont “returning”  
testcontin.rb:12: undefined method `call’ for “returning”:String  
(NoMethodError)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![david\_karapetyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@david\_karapetyan](https://www.ruby-forum.com/u/david_karapetyan)\
**Post date:** [June 7, 2007, 6:01am UTC](https://www.ruby-forum.com/t/code-problems/105868/18 "2007-06-07T06:01:07Z")

</div>

On 6/6/07, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 6/6/07, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > That is a good example I really like to build on it.  
> > Thanks.
> > 
> > Maybe I might add one thing which took me some minutes to work out 😉  
> > the nil value for the cont variable comes from : puts “After call”.
> 
> Actually it comes from the puts “After callcc” as this change demonstrates;  
> Stupid typo of mine, sorry  
> R.
