From: Calamitas Date: 2008-08-09T06:17:32+09:00 Subject: Re: Works in irb but not elsewhere On Fri, Aug 8, 2008 at 10:36 PM, Mikael Høilund wrote: > > 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 raise > a LocalJumpError as one would expect (since there's no block to yield inside > 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