More rebinding blocks

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?

-Mat

On Jun 16, 2006, at 12:46 PM, Mat S. wrote:

b.binding = self.binding

of instance_eval. But why not extend Proc to accept new bindings
instead?

Is there something I don’t know that maybe I should?

-Mat

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.)

On 6/16/06, Mat S. [email protected] wrote:

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.

Regards,
Sean

On Jun 16, 2006, at 12:56 PM, Logan C. wrote:

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.

Thanks,
Mat

On Jun 16, 2006, at 3:43 PM, Mauricio F. wrote:

one, Ara
eigenclass.org
where I also compare it to some other ones.

You, my friend, possess an impressive intellect. Thanks so much.
-Mat

On Sat, Jun 17, 2006 at 04:14:06AM +0900, Sean O’Halpin wrote:

On 6/16/06, Mat S. [email protected] wrote:

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.