# More rebinding blocks

**URL:** <https://www.ruby-forum.com/t/more-rebinding-blocks/63617>\
**Category:** Ruby\
**Created:** [June 16, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/more-rebinding-blocks/63617 "2006-06-16T18:48:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mat\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@Mat\_S](https://www.ruby-forum.com/u/Mat_S)\
**Post date:** [June 16, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/more-rebinding-blocks/63617/1 "2006-06-16T18:48:44Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Mat\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@Mat\_S](https://www.ruby-forum.com/u/Mat_S)\
**Post date:** [June 16, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/more-rebinding-blocks/63617/2 "2006-06-16T18:58:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mat\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@Mat\_S](https://www.ruby-forum.com/u/Mat_S)\
**Post date:** [June 16, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/more-rebinding-blocks/63617/3 "2006-06-16T21:15:37Z")

</div>

On 6/16/06, Mat S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Mat\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@Mat\_S](https://www.ruby-forum.com/u/Mat_S)\
**Post date:** [June 16, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/more-rebinding-blocks/63617/4 "2006-06-16T20:29:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mat\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@Mat\_S](https://www.ruby-forum.com/u/Mat_S)\
**Post date:** [June 16, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/more-rebinding-blocks/63617/5 "2006-06-16T21:56:38Z")

</div>

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

> one, Ara  
> [eigenclass.org](http://eigenclass.org/hiki.rb?instance_exec)  
> where I also compare it to some other ones.

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

---

<div class="post-metadata">

**Author:** ![Mat\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@Mat\_S](https://www.ruby-forum.com/u/Mat_S)\
**Post date:** [June 16, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/more-rebinding-blocks/63617/6 "2006-06-16T21:46:40Z")

</div>

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

> On 6/16/06, Mat S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://eigenclass.org/hiki.rb?instance_exec)  
where I also compare it to some other ones.
