Forum: Ruby on Rails Using closures, continuations in real life

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4aac499fafdb5e019d22ed857e731d2f?d=identicon&s=25 Fefo (Guest)
on 2007-03-05 04:19
(Received via mailing list)
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 :

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}"}
def greet(name) name

frenchGreeting = "Bonjour"
englishGreeting = "Hello"
southernGreeting = "Howdy"
japaneseGreeting = "Ohayou"
germanGreeting = "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

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

Espero sus opiniones y comentarios tambien sobre por ejemplo
6ebedec9056fcd5ecff378068cf03f83?d=identicon&s=25 Jimtron (Guest)
on 2007-03-05 16:45
(Received via mailing list)
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|
                 n * f[n-1]

Y[lil_fib][10] => 3628800

Continuations still screw me up big time. Chad Fowler and Jim Weirich
wrote this on it:
I still don't really grok the concept. I've heard that Seaside (a web
using Smalltalk) makes heavy use of continuations. Maybe playing with
that would help make the concept more concrete?

This topic is locked and can not be replied to.