Ruby 1.9 Binding#exec and introspection

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

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.

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