# Using closures, continuations in real life

**URL:** <https://www.ruby-forum.com/t/using-closures-continuations-in-real-life/87682>\
**Category:** Rails\
**Created:** [March 5, 2007, 4:19am UTC](https://www.ruby-forum.com/t/using-closures-continuations-in-real-life/87682 "2007-03-05T04:19:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fefo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba8739/32.png) [@Fefo](https://www.ruby-forum.com/u/Fefo)\
**Post date:** [March 5, 2007, 4:19am UTC](https://www.ruby-forum.com/t/using-closures-continuations-in-real-life/87682/1 "2007-03-05T04:19:26Z")

</div>

Hi all !! I’m fairly new to Rails programming and finding somewhat  
different n the approach than .NET programming I did previosuly. I  
found this blog post :  
[http://deadprogrammersociety.blogspot.com/2007/02/ruby-blocks-closure](http://deadprogrammersociety.blogspot.com/2007/02/ruby-blocks-closure)…

It’s about closures and contiinuations, I get the basic idea about  
closures for example  
class GreetingGenerator  
def initialize(greeting)  
@block = lambda {|name| puts “#{greeting}, #{name}”}  
end  
def greet(name)  
@block.call name  
end  
end

frenchGreeting = GreetingGenerator.new “Bonjour”  
englishGreeting = GreetingGenerator.new “Hello”  
southernGreeting = GreetingGenerator.new “Howdy”  
japaneseGreeting = GreetingGenerator.new “Ohayou”  
germanGreeting = GreetingGenerator.new “Guten Tag”

frenchGreeting.greet “John”  
englishGreeting.greet “John”  
southernGreeting.greet “John”  
japaneseGreeting.greet “John”  
germanGreeting.greet “John”

puts greeting # just checking that the `greeting` variable doesn’t  
exist in the main scope  
In this case, each block carries a little information with it  
(“greeting”).

This gives me the idea that clousures might be used in practice as a  
functional replacement for a class behaving as an abstract factory  
(the pattern). In this example the function makes a closure over the  
type of language (i.e. english, german) and then we pass to each  
function the parameters for the fabric to produce the output.  
Could anyone post some more comments and tell me if this idea is OK? I  
would love to know about continuations also. !

Thanks & regards  
Federico

Espero sus opiniones y comentarios tambien sobre por ejemplo  
continuations.

---

<div class="post-metadata">

**Author:** ![Fefo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ba8739/32.png) [@Fefo](https://www.ruby-forum.com/u/Fefo)\
**Post date:** [March 5, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/using-closures-continuations-in-real-life/87682/2 "2007-03-05T16:45:55Z")

</div>

What you’re doing seems fine to me, although I’m no expert.  
I sometimes see something like class\_eval used to write the  
entire method instead. Most of what little I know of the topic comes  
from the Little Schemer; here’s a pretty long way to make a  
factorial function in Ruby using closures:

Y = lambda { |le| lambda { |f| f[f] }[ lambda { |f| le[ lambda { |x|  
f[f][x] }]}]}

lil\_fib = lambda { |f|  
lambda { |n|  
if n.zero?  
1  
else  
n \* f[n-1]  
end  
}}

Y[lil\_fib][10] =\> 3628800

Continuations still screw me up big time. Chad F. and Jim W.  
wrote this on it: [Chicago Area Ruby Group - Continuations Demystified - Cover](http://onestepback.org/articles/callcc/)  
I still don’t really grok the concept. I’ve heard that Seaside (a web  
framework  
using Smalltalk) makes heavy use of continuations. Maybe playing with  
that would help make the concept more concrete?

Jim
