From: "Mikael Høilund" Date: 2008-08-09T05:36:57+09:00 Subject: Re: Works in irb but not elsewhere 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: >> @foo = :outside => :outside >> class Klass >> def initialize >> @foo = :inside >> end >> end >> Klass.new.instance_eval { @foo } => :inside # As expected >> def lol; Klass.new.instance_eval { yield } end >> lol do puts @foo end => :outside # o.O I might be completely missing something here, but I would expect that last line to raise that darn LocalJumpError. This shows that the “current block” inside the instance_eval is indeed what's passed to the caller. … >> def lol; Klass.new.instance_eval { Proc.new } end # Proc.new with no block returns the current block, cf. http://bit.ly/37NtlX >> my_proc = proc { :test } => # >> lol &my_proc => # >> lol(&my_proc) == my_proc => true This might be a bug. -- # Mikael Høilund def method_missing(m, a=0) a + m.to_s[/[a-z]+/].size * 2; end p What is the meaning of life?