I. E. Smith-Heisters
9/16/2008 7:24:00 PM
Hi everyone,
I want to do this:
x, y = %w(x y)
definition = lambda{def z; x; end}
y.extend Module.new(&definition)
y.z # => 'x'
I expected x to be available within the closure, but it doesn't appear
to be. The only way I've found is with module_eval:
x, y = %w(x y)
mod = Module.new
mod.module_eval "def z; #{x.inspect}; end"
y.extend mod
y.z # => 'x'
Which I guess is fine, but I'm opposed to eval, and especially
eval'ing strings, on principle.
So:
Why doesn't the first option work? I saw something somewhere about the
argument to Module::new being called within some other scope/context/
whatsit?
Is there a way to do this without eval?
Thanks!
-Ian