From: Phil Tomson Date: 2006-11-11T03:41:09+09:00 Subject: Re: accessing caller's binding implicitly On 11/9/06, Ben Bleything wrote: > Hi all, > > As part of my IRB shell history hack (shameless plug[1]), I need to be > able to access the caller's binding. Essentially, I need to be able to > do this: > > def irb_eval( lines, scope ) > eval(lines.join("\n"), scope) > end > > irb_eval( ['a = :foo', 'b = :bar'], binding ) > > ... but without explicitly passing the binding. The goal is this: > > irb_eval( ['a = :foo', 'b = :bar'] ) > > puts a > => :foo > > puts b > => :bar > > Is this even possible? As it is, the eval happens inside of irb_eval's > scope (I'm assuming... wherever it happens, I can't see the results when > it's done) which puts a cramp in my plans for world domination via cute > IRB hacks. > > Help? > > Thanks, > Ben > > [1]: http://blog.bleything.net/articles/2006/10/21/shell-style-history-for-irb > > Ben, I was thinking about this when you asked at the meeting the other night... Given that binding_of_caller seems to be broke in 1.8.5, would something like this work for you?: class IRB_Evaler def initialize scope @scope = scope end def irb_eval(lines) eval(lines.join("\n"),@scope) end end evaler = IRB_Evaler.new binding evaler.irb_eval( ['a = :foo', 'b = :bar'] ) puts a #=> foo Phil