In ruby there is such a thing as rebinding a proc object, as the example
below shows:
···
#####
class Foo
def initialize
@name = "anna"
end
@@hello = proc{ puts "hello #{@name}" }
define_method :hello, @@hello
end
f=Foo.new
f.hello
class Foo; @@hello[]; end
####
When you run the above, it writes:
hello anna
hello
A Proc obejct carries its binding with itself (ie. is a closure), as it
is well known. When @@hello was created, it did not know any @name. But
then it was passed to define_method, and the resulting method "hello"
can find @name in the instances. That is, ruby altered the bindings of
(some kind of copy of) @@hello, and only after that has it been attached
as a method. (Or rather, the binding is altered upon instantiation?)
But all of this happened under the hood.
What do you think of an explicit way of altering method bindings? I ask
it because I could have made good use of that now (I tell the concrete
example, is someone is interested). When one works with a bunch of
anonymous processes, it's not handy/feasible to make them methods just
for getting the binding changed.
* Would it be useful?
* A few lines of C which does it?
* Is it possible with EvilRuby?
* Is something like this planned (has it been discussed)?
Csaba