From: Gary Wright Date: 2007-11-07T12:12:39+09:00 Subject: Re: instance_eval to evaluate methods inside a block in context On Nov 6, 2007, at 8:46 PM, Adam Anderson wrote: > Can anyone tell me why this wouldn't/shouldn't/should work? I don't > want to pull out all of my hair trying to figure this out if it isn't > even possible. When you call instance_eval in Calculator#evaluate, all method calls without an explicit receiver are called relative to the calculator instance. The call to two() works because two() is defined as an instance method of Calculator. The call to hi() fails because hi() is an instance method of Foo not of Calculator. It is important to realize that method invocation with an instance_eval block is resolved dynamically based on self and not lexically based on where the block is declared (in this case within an instance method of Foo). By calling instance_eval you are explicitly overriding the normal binding of self within the block. Try changing howdy() as follows: > def howdy > Calculator.new.evaluate do > puts two > puts self.inspect # <== find out what self is > hi > end You'll see that self is an instance of calculator and hi() isn't defined for for Calculator objects, only for Foo objects. Gary Wright