Hi T.
(…) the core problem (…)
a = [1,2,3]
class_eval {
def ameth
p a
end
}(…) So how does your solution deal with this problem?
I thought you could tell from my examples, but let’s try it with your
exact problem…
You know you can pass objects to eval:
a = [1,2,3]
eval “p a”
results in
[1, 2, 3]
You can even use those objects in blocks:
a = [1,2,3]
eval( “proc{ p a }” ).call
also results in
[1, 2, 3]
But you can’t do so in a method definition:
a = [1,2,3]
self.class.class_eval “def ameth; p a; end”
ameth
results in
(eval):1:in ameth': undefined local variable or method a’ for
main:Object (NameError)
That’s because “a” is interpreted in the scope of the method definition,
where it must be a local variable or a method, which it isn’t.
So the trick is to use a proc as shown above and use it as a method,
which can be done with define_method:
a = [1,2,3]
self.class.class_eval “define_method :ameth, proc{ p a }”
ameth
results in
[1, 2, 3]
If I understood you right, this is what you wanted.
Regards,
Pit