Quantcast

Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Friday, June 09, 2006

Continuations with Ruby

I have been diving into continuations a bit lately and would like to share with you some of my findings. First of all, allow us to let the code speak for itself for a moment. Fire up irb and copy the following in:


class Foo
def bar
catch :break do
puts "starting..."
callcc do |@continuation|
puts "pausing..."
throw :break
end
puts "finished"
end
end
def continue
@continuation.call
puts "this puts will not ever be executed"
end
end

f = Foo.new
f.bar
# => starting...
# => pausing...
f.continue
# => finished


You will notice a few things about this code. First is that callcc
takes a parameter but does not use it. Rather, that parameter
actually becomes a block in it of itself which is called in the
continue method. Next is that within the callcc block we catch a
symbol. We do this specifically and only with the intent of stopping
the flow of the code. The final thing to notice is that as soon as you call the @continuation block, it sends you right back into the bar method to the point after the callcc block is made and continues along its merry way... it does not hop back over to the continue method when finished... it actually transfers into a whole new scope.

If this is new to you, chew on it for a while and enjoy. If this is
old hat, then I have more continuations cooking for you soon. To give
you a hint, there are plenty of ActiveRecord plugins out there but I
am thinking of building on of the first ActionController plugins. I
call it acts_as_continuation.

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!

5 Comments:

Anonymous Avdi said...

Instead of abusing exceptions, why not use throw/catch? Unlike exceptions, throw/catch is actually intended to be a control-flow tool, and is very useful in conjunction with callcc.

8:15 AM, June 09, 2006

 
Blogger Lucas Carlson said...

You are correct, I have updated the example to reflect this.

9:00 AM, June 09, 2006

 
Blogger Zhenhong Bao said...

michael kors outlet clearance
tiffany jewelry
tiffany jewelry
swarovski crystal
ralph lauren polo
ralph lauren,polo ralph lauren,ralph lauren outlet,ralph lauren italia,ralph lauren sito ufficiale
tiffany and co jewelry
ferragamo shoes
hollister uk
ferragamo shoes
kobe shoes
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
tiffany and co
toms outlet
cheap ray ban sunglasses
polo ralph lauren
cheap mlb jerseys
true religion sale
cheap jordan shoes
ralph lauren uk
polo ralph lauren
michael kors outlet online
mont blanc pens
rolex watches
ray-ban sunglasses
longchamp outlet
louis vuitton handbags
iphone case
burberry outlet online
juicy couture outlet
christian louboutin uk
omega watches
true religion jeans outlet
ferragamo shoes
bottega veneta outlet
20160531zhenhong

6:05 PM, May 30, 2016

 
Blogger محمد الخطيب said...

شركة عزل اسطح بالرياض
شركة عزل خزانات المياه بالرياض
شركه عزل مائى بالرياض
شركة عزل حرارى بالرياض شركة عزل اسطح بالدمام
شركة عزل خزانات المياه بالدمام
شركه عزل مائى بالدمام
شركة عزل حرارى بالدمام شركة عزل اسطح بالاحساء
شركة عزل خزانات المياه بالاحساء
شركه عزل مائى بالاحساء
شركة عزل حرارى بالاحساء شركة عزل اسطح بالجبيل
شركة عزل خزانات المياه بالجبيل
شركه عزل مائى بالجبيل
شركة عزل حرارى بالجبيل شركة عزل اسطح بالخبر
شركة عزل خزانات المياه بالخبر
شركه عزل مائى بالخبر
شركة عزل حرارى بالخبر
شركة ترميم منازل بالخبر شركة عزل اسطح بالقصيم
شركة عزل خزانات المياه بالقصيم
شركه عزل مائى بالقصيم
شركة عزل حرارى بالقصيم شركة عزل اسطح بالقطيف
شركة عزل فوم بالقطيف
شركه عزل مائى بالقطيف
شركة عزل حرارى بالقطيف

4:33 PM, July 04, 2016

 
Blogger Hua Cai said...

ugg boots sale
coach outlet online
instyler
cheap replica watches
burberry outlet
hollister clothing store
adidas uk
kobe 9 elite
jordan shoes
herve leger outlet
lebron shoes
ferragamo shoes
lebron james shoes
nike roshe run
nike air max 90
michael kors wallet sale
louis vuitton bags
cheap jordan shoes
tiffany jewelry
fitflops uk
michael kors handbags
longchamp bag
cartier sunglasses for men
lululemon outlet
jordan pas cher
futbol baratas
oakley sunglasses
ugg outlet
celine outlet
swarovski crystal
ray ban sunglasses
true religion uk outlet
versace sunglasses on sale
true religion outlet
cheap oakley sunglasses
20160804caihuali

10:40 PM, August 03, 2016

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

If you like this blog, you might also like top photography schools.