As I’m a fan of functional languages as well, feeling comfortable with
languages such as OCaml (which seems to be a favorite other language for
many people on this list), I’ve noticed that Ruby’s blocks look
suspiciously like lambda abstractions in functional languages, just with
a very quirky syntax. They’re pretty much used in the same way, as
parameters to functions, although they’re far more limited in scope than
a lambda would be in a Scheme or OCaml, and require somewhat special
treatment. What if we made a block a first class entity, allowing one
to assign a block to a variable, and pass a block as a formal parameter
to a method? This of course would also mean making a block an object,
which in my opinion is a very powerful idea. So you might have some
code that looks like this:
def foo(b1, b2)
b1.execute(a, b, c)
b2.execute(d, e)
end
tmp_blk = { |a, b| … }
foo({ |a, b, c| … }, tmp_blk)
{|a| … }.execute(b)
The yield builtin keyword would do the same thing as invoking the
execute method on the first (possibly implicitly specified) parameter.
What do you guys think?
···
–
Rafael R. Sevilla +63(2)8123151
Software Developer, Imperium Technology Inc. +63(917)4458925