Sorry to beat a dead horse, here. But after reading through the
older discussions on google groups, I’m still left with a question.
Arbitrarily rebinding blocks seems like it would be amazingly
useful. Is there some fundamental reason that the following code is
impossible? Or has it just not been implemented yet.
class MyClass
def runhere(b) @a = 3
b.binding = self.binding
b.call
end
end
MyClass.new.runhere(proc { puts @a })
I know instance_eval can do this simple case, but as covered in
previous conversations (http://tinyurl.com/ehq8x [google ruby-talk]),
you loose the ability pass arguments to the block. Matz proposed an
UnboundMethod workaround. Dave proposed an extension of
instance_eval. But why not extend Proc to accept new bindings instead?
Is there something I don’t know that maybe I should?
Can Matz or anyone offer any assurance that that feature of 1.9 will
survive in later releases? I’d hate to write code that uses it only
to loose it in a future release.
I think you can be pretty sure it will survive. You can also find an
implementation by Ara Howard for 1.8 in ruby-talk:156726.
def runhere(b) @a = 3
b.binding = self.binding
b.call
end
end
MyClass.new.runhere(proc { puts @a })
1.9 has #instance_exec which is #instance_eval + the ability to
pass args. (The urge for me to actually check out 1.9 grows daily.)
Oh nice. That would work. It doesn’t exactly answer my question,
though. But I guess the answer could just be that the community
didn’t decide on that particular syntax.
Can Matz or anyone offer any assurance that that feature of 1.9 will
survive in later releases? I’d hate to write code that uses it only
to loose it in a future release.
Can Matz or anyone offer any assurance that that feature of 1.9 will
survive in later releases? I’d hate to write code that uses it only
to loose it in a future release.
I think you can be pretty sure it will survive. You can also find an
implementation by Ara Howard for 1.8 in ruby-talk:156726.
There are many implementations around. In addition to the above one, Ara
posted another on [[ruby-core:6380]. Both are thread-safe, but will not
work
in general with frozen objects. They’re still better than the one you
can find
in ActiveSupport, which is not strictly thread-safe (it is in practice,
though).
I wrote yet another implementation which is thread-safe and works with
frozen objects; you can find it at eigenclass.org
where I also compare it to some other ones.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.