Calamitas
8/8/2008 9:18:00 PM
On Fri, Aug 8, 2008 at 10:36 PM, Mikael H=F8ilund <mikael@hoilund.org> wrot=
e:
>
> On Aug 8, 2008, at 22:16, Patrick Li wrote:
>
>> Thanks for all your comments:
>>
>> I think there's a bug in how ruby treats block parameters.
>>
>> ie. the following works
>> def myMethod &block
>> A.new.instance_eval &block
>> end
>>
>> but this doesn't
>> def myMethod
>> A.new.instance_eval{yield}
>> end
>>
>
> For reasons I cannot quite understand at the moment, the block for
> instance_eval is executed in the scope of the receiver, but yield inside
> that block will cause the block given to the *caller* to execute, not rai=
se
> a LocalJumpError as one would expect (since there's no block to yield ins=
ide
> the object). That block is executed at its original scope:
The only effect of instance_eval on the block it gets as parameter is
that self is changed. It influences only instance variables and method
calls without explicit receiver, nothing else. yield is tied to the
method it is in, not what self is at that point, so it is not affected
by instance_eval.
This should also answer the OP's question: since yield is unaffected,
it doesn't change self in the block it calls. So it's definitely not a
bug. You're calling #method on the toplevel object, which is an
instance of Object, and Object#method expects 1 parameter.
Peter