Trans
11/10/2006 4:46:00 AM
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?
Unfortuately not. The binding-of-caller hack is "out of order" last I
heard. The only trick you have available that's a sure thing is to
prepend a block to the end of the call. Eg.
irb_eval( ['a = :foo', 'b = :bar'] ){}
You can get the scope off of the block via Proc#binding.
I have to comment that since this is so readily done by passing a
block, it seems like it should be trivial just to go ahead and make
binding of caller a built in metaprogramming method. You know, Matz
talks about being tired of the little things and the glacier pace of
Ruby's development, but it's little things like this that people have
been requesting again and again for years.
T.