# Ruby 1.9 Binding#exec and introspection

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-binding-exec-and-introspection/144415>\
**Category:** Ruby\
**Created:** [July 29, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/ruby-1-9-binding-exec-and-introspection/144415 "2008-07-29T13:23:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hetman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cab0a1/32.png) [@hetman](https://www.ruby-forum.com/u/hetman)\
**Post date:** [July 29, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/ruby-1-9-binding-exec-and-introspection/144415/1 "2008-07-29T13:23:45Z")

</div>

I’d like to open up a discussion about extending the Binding class we  
have available in Ruby. Apologies if this is already happening somewhere  
but I couldn’t find any discussions on the topic.

I think it’s great Ruby 1.9 now has added Binding#eval. Having access to  
the local binding is a powerful tool and it would be great to see it  
become more accessible.

I think it would be nice to see something like the new class\_exec and  
instance\_exec methods for the Binding class so we don’t have to go  
through string evals. Specifically a Binding#exec

Some introspective abilities would also be a bonus. The most obvious  
ones I can think of are: Binding#local\_variables,  
Binding#local\_variable\_get

There’s probably more information we can pull out of the local binding  
but these two are things I really miss from Python.

What do you guys think, let’s hear some ideas.

Cheers

---

<div class="post-metadata">

**Author:** ![hetman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cab0a1/32.png) [@hetman](https://www.ruby-forum.com/u/hetman)\
**Post date:** [July 31, 2008, 9:34am UTC](https://www.ruby-forum.com/t/ruby-1-9-binding-exec-and-introspection/144415/2 "2008-07-31T09:34:57Z")

</div>

Hi,

At Tue, 29 Jul 2008 20:23:14 +0900,  
Przemek Wrzos wrote in [ruby-talk:309468]:

> I think it would be nice to see something like the new class\_exec and  
> instance\_exec methods for the Binding class so we don’t have to go  
> through string evals. Specifically a Binding#exec

I suspect it won’t work as you expect. At the time block is  
parsed, where scope Binding refers is unknown, so you can’t use  
local variables of the binding from the block.

> Some introspective abilities would also be a bonus. The most obvious  
> ones I can think of are: Binding#local\_variables,  
> Binding#local\_variable\_get

I guess it would be better to add those methods instead of  
eval.

---

<div class="post-metadata">

**Author:** ![hetman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cab0a1/32.png) [@hetman](https://www.ruby-forum.com/u/hetman)\
**Post date:** [August 3, 2008, 10:37am UTC](https://www.ruby-forum.com/t/ruby-1-9-binding-exec-and-introspection/144415/3 "2008-08-03T10:37:02Z")

</div>

Hi nobu,

Hmm, I see what you’re saying about the binding being unknown at parse  
time. I think a Binding#exec method might still be possible but some  
extra work would be needed. Specifically the parser would have to  
generate a special dynamic type of binding object which would re-route  
the block’s variables to the binding made available to it through #exec  
at runtime.

This is starting to sound like a non-trivial problem though, I’m not  
sure it’s actually worth the effort since you can get most of the  
benefits with some additional interface methods. Plus it would be tricky  
making this behaviour consistent with the rest of the language…

I was thinking about what instance methods Binding might make available  
based on how Object treats instance variables. You’d probably need  
these:

Binding#variable\_defined?  
Binding#variable\_get  
Binding#variable\_set  
Binding#variables

I’m not sure if what I proposed earlier would be possible because the  
parser might not make a specific distinction here, but the same sort of  
methods might also be helpful for #local\_variables and  
#global\_variables.

On a side note. I would like to know how to take up this sort of  
discussion further with the developers of the language. Is there a  
formal proposal submission process that is still being used?

Also, how would I go about maybe submitting a patch if I was interested  
in delving into the source code my self to implement something like  
this?

Cheers
