Proc#binding?

Just a thinking out loud here, I have no pressing need for this, but…
We’ve got Kernel#binding which returns the Binding object of the current
execution context. It would seem that Proc objects also have bindings as
well, but there is no Proc#binding method that allows us to access them.
Should there be?

Actually, come to think of it, for this to be useful there would probably
have be some methods in the Binding class - methods that would return
lists of classes, variables and methods defined in a particular binding,
for example. Maybe even comparison operators to check if two bindings are
equivilent, etc… perhaps even the ability to serialize a binding (which
could lead to serializing Proc’s too, I suppose) which of course probably
leads to a slippery slope…

Phil

Phil Tomson wrote:

Just a thinking out loud here, I have no pressing need for this, but…
We’ve got Kernel#binding which returns the Binding object of the current
execution context. It would seem that Proc objects also have bindings as
well, but there is no Proc#binding method that allows us to access them.
Should there be?

Seems to be there in 1.7.3. But you could always implement it with eval:

class Proc
def get_binding
eval “binding”, self
end
end

class A
def foo
x = 1
Proc.new {}
end
end

pr = A.new.foo

p (eval “x”, pr.binding)
p (eval “x”, pr.get_binding)

In article 3DD6AC3E.1020804@path.berkeley.edu,

Phil Tomson wrote:

Just a thinking out loud here, I have no pressing need for this, but…
We’ve got Kernel#binding which returns the Binding object of the current
execution context. It would seem that Proc objects also have bindings as
well, but there is no Proc#binding method that allows us to access them.
Should there be?

Seems to be there in 1.7.3. But you could always implement it with eval:

class Proc
def get_binding
eval “binding”, self
end
end

class A
def foo
x = 1
Proc.new {}
end
end

pr = A.new.foo

p (eval “x”, pr.binding)
I think this will report a private method access error.

p (eval “x”, pr.get_binding)

Hmmm… actually even in 1.7.3 Proc#binding seems to be a private
method:

irb(main):001:0> a = proc { i = 5; puts i; }
#Proc:0x401fb380
irb(main):002:0> a.binding
NameError: private method `binding’ called for #Proc:0x401fb380
from (irb):2

but it looks like your get_binding method definition will work.

Phil

···

Joel VanderWerf vjoel@PATH.Berkeley.EDU wrote:

Phil Tomson wrote:

Hmmm… actually even in 1.7.3 Proc#binding seems to be a private
method:

irb(main):001:0> a = proc { i = 5; puts i; }
#Proc:0x401fb380
irb(main):002:0> a.binding
NameError: private method `binding’ called for #Proc:0x401fb380
from (irb):2

but it looks like your get_binding method definition will work.

Phil

Must have changed recently…

irb(main):001:0> a = proc { i = 5; puts i; }
#Proc:0x401f4390@:1(irb)
irb(main):002:0> a.binding
#Binding:0x401f07b8
irb(main):003:0> RUBY_VERSION
“1.7.3”
irb(main):004:0> RUBY_RELEASE_DATE
“2002-10-30”

Something else I observe here is that Proc#inspect seems to be telling
us what line (of irb input) the proc was defined on. I wonder what other
information is stored in Procs nowadays…