Robert Klemme
5/27/2006 11:07:00 PM
tjmkiely@gmail.com wrote:
> I have a basic question about expressions as parameters to blocks.
>
> If I pass and expression as the parameter to a block, is it possible to
> get the string representation of the expression before evaluation.
>
> For example, with the following block:
>
> write = proc { |x| file.write("x:#{x})")}
>
> If called like this:
>
> write[request.domain]
>
> I would like to have this printed:
>
> request.domain: localhost
What you want is not possible with normal means. The simplest solution
would be to hand over the expression as string but then you also need to
provide the binding. Something along the lines
write = lambda {|expr, bind| file.write "#{expr}:#{eval(expr,bind)}" }
More complex solutions involve parsing the source code.
Cheers
robert