I would like to write a function which evaluates
a block in the ontext of an object.
I tried something like that (of course it did not work):
class MyClass
attr_reader :test
end
def f(&block)
x = MyClass.new
x.instance_eval(&block)
x
end
puts( (f { @test = 1 } ).test )
I would like to achieve that the code block of f
is evaluated in the context of the new MyClass object.
It is evaluated in the main context.
Is there a way to rewrite my f function so that
the last line of the code prints 1 instead of nil,
or is it pricipially impossible in Ruby?
Regards, Christian