Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Continuation in ruby

Reply
Thread Tools

Continuation in ruby

 
 
Minkoo Seo
Guest
Posts: n/a
 
      02-14-2006
Hi all.

I've written the following code to learn continuations in ruby only to
fail. Please have a look at:


def print_num
for i in (1..10)
puts i
if i % 2 == 0; callcc{|c| return c}; end
end
end

cont = print_num
puts "Yae, I love even~"
cont.call
puts "Bye"


This code produces:

C:\WINDOWS\system32\cmd.exe /c ruby a.rb
1
2
Yae, I love even~
3
4
Yae, I love even~
5
6
Yae, I love even~
7
8
Yae, I love even~
9
10
Yae, I love even~
Yae, I love even~
a.rb:10: undefined method `call' for 1..10:Range (NoMethodError)
shell returned 1
Hit any key to close this window...

This is contrary to my intuition. I've called cont.call only once, and
cont.call should put the following command, put "bye", into stack. I
can't figure out the reason why cont.call is called indefinitely.

Best,
Minkoo Seo

 
Reply With Quote
 
 
 
 
Jesse Yoon
Guest
Posts: n/a
 
      02-14-2006
Minkoo,

When you call cont.call, the control goes right back to the end of
callcc block with all the context in tact. So your program's control
flow looks like this:

1. cont = print_num # calls print_num method
2. callcc {|c| return c} # at i % 2 == 0, print_num returns with the
continuation as a return value, which is now assigned to cont
3. puts "Yae..."
4. cont.call # now the control goes back to "}" of callcc with the
variable i == 2
5. for loop continues
6. go to step 2 unless i == 10
7. at i == 10, callcc block returns one final time
8. puts ...
9. cont.call
10. now that i == 10 for loop finishes and the return value of
print_num at this time is (1..10), because "for loop" is essentially
(1..10).each {|i|} in disguise, and #each returns self
11. puts ...
12. cont.call # argh! this time you're calling (1..10).call
13. NoMethodError exception

This is what has happened.

Hope this helps.

Jesse

Minkoo Seo wrote:
> Hi all.
>
> I've written the following code to learn continuations in ruby only to
> fail. Please have a look at:
>
>
> def print_num
> for i in (1..10)
> puts i
> if i % 2 == 0; callcc{|c| return c}; end
> end
> end
>
> cont = print_num
> puts "Yae, I love even~"
> cont.call
> puts "Bye"
>
>
> This code produces:
>
> C:\WINDOWS\system32\cmd.exe /c ruby a.rb
> 1
> 2
> Yae, I love even~
> 3
> 4
> Yae, I love even~
> 5
> 6
> Yae, I love even~
> 7
> 8
> Yae, I love even~
> 9
> 10
> Yae, I love even~
> Yae, I love even~
> a.rb:10: undefined method `call' for 1..10:Range (NoMethodError)
> shell returned 1
> Hit any key to close this window...
>
> This is contrary to my intuition. I've called cont.call only once, and
> cont.call should put the following command, put "bye", into stack. I
> can't figure out the reason why cont.call is called indefinitely.
>
> Best,
> Minkoo Seo


 
Reply With Quote
 
 
 
 
G.Durga Prasad
Guest
Posts: n/a
 
      02-14-2006
On 2/14/06, Minkoo Seo <> wrote:
> Hi all.
>
> I've written the following code to learn continuations in ruby only to
> fail. Please have a look at:
>


From another learner
Look at my following try
def print_num
for i in (1..10)
puts i
if i % 2 =3D=3D 0; callcc{|c| return c}; end
puts "** cont.call brings you here"
end
return nil
end

cont =3D print_num
puts "Yae, I love even~"
cont.call if cont
puts "Bye"

Hope this helps
Prasad


 
Reply With Quote
 
Minkoo Seo
Guest
Posts: n/a
 
      02-14-2006
Now I get it.

So the continuation returned from print_num by cont = print_num
contains not only the context within print_num method, but also the
context of "cont = print_num". In other words, cont contains (1)
print_num was called, (2) location within print_num, (3) and variables
in its stack.

Thank you, folks.

Best,
Minkoo Seo

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby annoyances ... mine is line continuation, what's yours? scooterm@hotmail.com Ruby 10 01-19-2008 03:16 AM
help me to split the display of the contents of an ASP page into 2 pages by having the continuation aarthy kumar ASP .Net 3 10-12-2005 12:30 PM
Meta Information for Ruby -- Continuation of the discussion about testNG needing function attributes, etc. Jeff Wood Ruby 1 09-02-2005 03:48 PM
Continuation question.... DC ASP .Net 2 05-24-2005 03:59 PM
why window.open script not firing? just postback...continuation of previous post KathyB ASP .Net 2 07-17-2003 02:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57