Louis-Philippe said:
I'm not sure I'll use it though, as it require you define the function
inside a string...
a bit cumbersome.
How about using %{ ... } for the string? Then it looks more like a
normal lambda.
a = fn %{ |x| puts x+x }
a["Hello"]
puts a.source
Or you can read the source from its own file, or for longer snippets you
can use a here-doc.
a = fn <<'EOF'
{ |x| puts x }
EOF
what I need is some sort of macro system (the LISP macro, not the C one),
to parse the block before calling it. I was thinking to make it a string,
then parsing with regex, would have done it...
Ruby doesn't have macros. If you write a block in the usual way, the
Ruby interpreter will turn it into a Block object, and you cannot get
its source form back (in MRI anyway)
If you are only interested in simple regexp source transformations, then
you should start from a string form as above, transform, then eval.
For more complex transformations, still starting with a string, you
might be able to use one of the ruby-in-ruby implementations (e.g.
rubinius) or ParseTree to parse and transform it, I'm not sure.