# Help with tricky proc/binding issue

**URL:** <https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924>\
**Category:** ruby-talk\
**Created:** [19 September 2005 14:42 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924 "2005-09-19T14:42:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 September 2005 14:42 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/1 "2005-09-19T14:42:12Z")

</div>

i'm trying to be able to define a proc that can be called in the context of  
self, eg:

&nbsp;&nbsp;&nbsp;block = lambda{ p 42 }  
&nbsp;&nbsp;&nbsp;an\_obj.instance\_eval &block

but which can also be called with arguments, for example (imaginary ruby):

&nbsp;&nbsp;&nbsp;b = lambda{|x| p [x, y]}

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def y; 42; end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;c = C::new  
&nbsp;&nbsp;&nbsp;x = 42

&nbsp;&nbsp;&nbsp;c.arg\_instance\_eval(x, &b) #=\> [42, 42]

any ideas?

-a

> **···**
>
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [19 September 2005 14:54 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/2 "2005-09-19T14:54:27Z")

</div>

Hi,

> **···**
>
> In message "Re: help with tricky proc/binding issue" on Mon, 19 Sep 2005 23:42:12 +0900, "Ara.T.Howard" \<Ara.T.Howard@noaa.gov\> writes:
> 
> > i'm trying to be able to define a proc that can be called in the context of  
> > self, eg:
> > 
> > &nbsp;&nbsp;block = lambda{ p 42 }  
> > &nbsp;&nbsp;an\_obj.instance\_eval &block
> > 
> > but which can also be called with arguments, for example (imaginary ruby):
> 
> I understand what you want. But I haven't come up with a good name  
> for the function yet.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [19 September 2005 15:01 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/3 "2005-09-19T15:01:37Z")

</div>

Ara.T.Howard wrote:

> i'm trying to be able to define a proc that can be called in the  
> context of self, eg:
> 
> &nbsp;&nbsp;&nbsp;block = lambda{ p 42 }  
> &nbsp;&nbsp;&nbsp;an\_obj.instance\_eval &block
> 
> but which can also be called with arguments, for example (imaginary  
> ruby):
> 
> &nbsp;&nbsp;&nbsp;b = lambda{|x| p [x, y]}
> 
> &nbsp;&nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def y; 42; end  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;c = C::new  
> &nbsp;&nbsp;&nbsp;x = 42
> 
> &nbsp;&nbsp;&nbsp;c.arg\_instance\_eval(x, &b) #=\> [42, 42]
> 
> any ideas?

The only workaround that comes to mind is to pass self explicitely along  
with other arguments. Or use a trick with delegation that allows to do  
instance\_eval on a proxy self that will also provide arguments given.

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [19 September 2005 15:39 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/4 "2005-09-19T15:39:42Z")

</div>

Ara.T.Howard schrieb:

> i'm trying to be able to define a proc that can be called in the context of  
> self, eg:
> 
> &nbsp;&nbsp;block = lambda{ p 42 }  
> &nbsp;&nbsp;an\_obj.instance\_eval &block
> 
> but which can also be called with arguments, for example (imaginary ruby):
> 
> &nbsp;&nbsp;b = lambda{|x| p [x, y]}
> 
> &nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;def y; 42; end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;c = C::new  
> &nbsp;&nbsp;x = 42
> 
> &nbsp;&nbsp;c.arg\_instance\_eval(x, &b) #=\> [42, 42]
> 
> any ideas?

Tom (aka Trans) recently mentioned that he changed procs into methods in order to get what you want. Here's a quick hack, not efficient and not thread-safe, but maybe a start:

&nbsp;&nbsp;&nbsp;class Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def arg\_instance\_eval( \*args, &block )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = class \<\< self; self; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :define\_method, :arg\_instance\_eval\_method, &block )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg\_instance\_eval\_method( \*args )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :remove\_method, :arg\_instance\_eval\_method )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;b = lambda{|x| p [x, y]}

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def y; 42; end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;c = C::new  
&nbsp;&nbsp;&nbsp;z = 41

&nbsp;&nbsp;&nbsp;c.arg\_instance\_eval( z, &b ) # =\> [41, 42]

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Florian\_Gross2](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@Florian\_Gross2](https://rubytalk.org/u/Florian_Gross2)\
**Post date:** [19 September 2005 15:41 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/5 "2005-09-19T15:41:22Z")

</div>

Ara.T.Howard wrote:

> i'm trying to be able to define a proc that can be called in the context of  
> self, eg:
> 
> &nbsp;&nbsp;block = lambda{ p 42 }  
> &nbsp;&nbsp;an\_obj.instance\_eval &block
> 
> but which can also be called with arguments, for example (imaginary ruby):

Currently the only way of doing this is Proc#self= from evil.rb...

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 September 2005 15:34 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/6 "2005-09-19T15:34:48Z")

</div>

maybe

&nbsp;&nbsp;&nbsp;class Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def evaluate(\*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

??

another option would be to turn the problem inside out and do something like

&nbsp;&nbsp;&nbsp;l = lambda{ p x }  
&nbsp;&nbsp;&nbsp;l.self = obj  
&nbsp;&nbsp;&nbsp;l.call

this is what i have so far:

&nbsp;&nbsp;&nbsp;[ahoward@localhost ~]$ cat a.rb  
&nbsp;&nbsp;&nbsp;class Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def evaluate(\*a, &b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = "\_\_\_\_eval\_\_\_\_#{ rand(65536) }\_\_\_\_"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = Class === self ? self : self::class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval{ define\_method m, &b }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send m, \*a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval{ remove\_method m }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;b = lambda{|x| p [x, y, self]}

&nbsp;&nbsp;&nbsp;c = C::new  
&nbsp;&nbsp;&nbsp;x = 42  
&nbsp;&nbsp;&nbsp;c.evaluate(x, &b) #=\> [42, 42, #\<C:0xb7f2f5c0\>]

&nbsp;&nbsp;&nbsp;[ahoward@localhost ~]$ ruby a.rb  
&nbsp;&nbsp;&nbsp;[42, 42, #\<C:0xb7f1b5c0\>]

thoughts?

-a

> **···**
>
> On Mon, 19 Sep 2005, Yukihiro Matsumoto wrote:
> 
> > Hi,
> > 
> > In message "Re: help with tricky proc/binding issue" \> on Mon, 19 Sep 2005 23:42:12 +0900, "Ara.T.Howard" \<Ara.T.Howard@noaa.gov\> writes:
> > 
> > \>i'm trying to be able to define a proc that can be called in the context of  
> > \>self, eg:  
> > \>  
> > \> block = lambda{ p 42 }  
> > \> an\_obj.instance\_eval &block  
> > \>  
> > \>but which can also be called with arguments, for example (imaginary ruby):
> > 
> > I understand what you want. But I haven't come up with a good name  
> > for the function yet.
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Florian\_Gross2](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@Florian\_Gross2](https://rubytalk.org/u/Florian_Gross2)\
**Post date:** [19 September 2005 15:41 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/7 "2005-09-19T15:41:42Z")

</div>

Yukihiro Matsumoto wrote:

> \>i'm trying to be able to define a proc that can be called in the context of  
> \>self, eg:  
> \>  
> \> block = lambda{ p 42 }  
> \> an\_obj.instance\_eval &block  
> \>  
> \>but which can also be called with arguments, for example (imaginary ruby):
> 
> I understand what you want. But I haven't come up with a good name  
> for the function yet.

What about Proc#with\_self() that will return a new proc similar to the current one, but with a changed self context?

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [19 September 2005 16:06 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/8 "2005-09-19T16:06:37Z")

</div>

Pit Capitain wrote:

> Ara.T.Howard schrieb:
> 
> > i'm trying to be able to define a proc that can be called in the  
> > context of self, eg:
> > 
> > &nbsp;&nbsp;block = lambda{ p 42 }  
> > &nbsp;&nbsp;an\_obj.instance\_eval &block
> > 
> > but which can also be called with arguments, for example (imaginary  
> > ruby):
> > 
> > &nbsp;&nbsp;b = lambda{|x| p [x, y]}
> > 
> > &nbsp;&nbsp;class C  
> > &nbsp;&nbsp;&nbsp;&nbsp;def y; 42; end  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;c = C::new  
> > &nbsp;&nbsp;x = 42
> > 
> > &nbsp;&nbsp;c.arg\_instance\_eval(x, &b) #=\> [42, 42]
> > 
> > any ideas?
> 
> Tom (aka Trans) recently mentioned that he changed procs into methods  
> in order to get what you want. Here's a quick hack, not efficient and  
> not thread-safe, but maybe a start:
> 
> &nbsp;&nbsp;&nbsp;class Object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def arg\_instance\_eval( \*args, &block )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = class \<\< self; self; end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :define\_method, :arg\_instance\_eval\_method, &block )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg\_instance\_eval\_method( \*args )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :remove\_method, :arg\_instance\_eval\_method )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end

Hacky but nice! For thread safety how about:

&nbsp;&nbsp;&nbsp;class Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def arg\_instance\_eval( \*args, &block )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = class \<\< self; self; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meth = "\_\_m\_#{Thread.current.object\_id}\_#{rand 666}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :define\_method, meth, &block )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send( meth, \*args )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :remove\_method, meth )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 September 2005 16:10 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/9 "2005-09-19T16:10:53Z")

</div>

that's funny. here's my current hack:

&nbsp;&nbsp;&nbsp;class Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def evaluate(\*a, &b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret, sent = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = "\_\_\_\_evaluate\_\_\_\_#{ rand(42) }\_\_\_\_#{ rand(42) }\_\_\_\_"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = Class === self ? self : self::class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval{ define\_method m, &b }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret = send(sent = m, \*a)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval{ remove\_method m }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if sent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;b = lambda{|x| p [x, y, self]}

&nbsp;&nbsp;&nbsp;c = C::new  
&nbsp;&nbsp;&nbsp;x = 42  
&nbsp;&nbsp;&nbsp;c.evaluate(x, &b) #=\> [42, 42, #\<C:0xb7f2f5c0\>]

guess that's the way to go for now.

cheers.

-a

> **···**
>
> On Tue, 20 Sep 2005, Pit Capitain wrote:
> 
> > > any ideas?
> > 
> > Tom (aka Trans) recently mentioned that he changed procs into methods in  
> > order to get what you want. Here's a quick hack, not efficient and not  
> > thread-safe, but maybe a start:
> > 
> > class Object  
> > &nbsp;&nbsp;&nbsp;def arg\_instance\_eval( \*args, &block )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = class \<\< self; self; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :define\_method, :arg\_instance\_eval\_method, &block )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg\_instance\_eval\_method( \*args )  
> > &nbsp;&nbsp;&nbsp;ensure  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.send( :remove\_method, :arg\_instance\_eval\_method )  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > b = lambda{|x| p [x, y]}
> > 
> > class C  
> > &nbsp;&nbsp;&nbsp;def y; 42; end  
> > end
> > 
> > c = C::new  
> > z = 41
> > 
> > c.arg\_instance\_eval( z, &b ) # =\> [41, 42]
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Florian\_Gross2](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@Florian\_Gross2](https://rubytalk.org/u/Florian_Gross2)\
**Post date:** [19 September 2005 15:52 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/10 "2005-09-19T15:52:36Z")

</div>

Ara.T.Howard wrote:

> another option would be to turn the problem inside out and do something like
> 
> &nbsp;&nbsp;l = lambda{ p x }  
> &nbsp;&nbsp;l.self = obj  
> &nbsp;&nbsp;l.call

Procs are currently immutable and I'd assume that the source code assumes that they can not be changed in several places. It's less risky to return a new object with a changed self context, IMHO.

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [19 September 2005 16:17 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/11 "2005-09-19T16:17:13Z")

</div>

I've talked about this before, but the ability to return a new  
proc with local variables manipulated would also be nice:

- Make "local" variables really local to the proc. Initialize  
these to the current values of the local variables in the  
previous binding. Effectively, this gives the proc its own  
local binding (at least for "local" variables). This could be  
done in conjunction with redefining self for the proc to.

It would also be nice to be able to be able to do the same  
thing for blocks easily so that you could easily localize your  
"local" variables in your block. But, you could emulate that  
as long as you had a way to localize proc local variables:

foo(&(lambda{...}.localize))

or if you could do it directly to a block you may have some  
syntax like this:

foo {{...}} # double braces to represent localization

But, that has some conflicts with hash. Not sure of a good  
syntax. The problem is we can't apply methods directly to  
blocks - you have to convert to Proc, apply the method, and  
convert back.

> **···**
>
> --- Florian Groß \<florgro@gmail.com\> wrote:
> 
> > Yukihiro Matsumoto wrote:
> > 
> > \> \>i'm trying to be able to define a proc that can be called  
> > in the context of  
> > \> \>self, eg:  
> > \> \>  
> > \> \> block = lambda{ p 42 }  
> > \> \> an\_obj.instance\_eval &block  
> > \> \>  
> > \> \>but which can also be called with arguments, for example  
> > (imaginary ruby):  
> > \>  
> > \> I understand what you want. But I haven't come up with a  
> > good name  
> > \> for the function yet.
> > 
> > What about Proc#with\_self() that will return a new proc  
> > similar to the  
> > current one, but with a changed self context?
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 September 2005 16:21 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/12 "2005-09-19T16:21:37Z")

</div>

^^^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^

lol! 😉

-a

> **···**
>
> On Tue, 20 Sep 2005, Robert Klemme wrote:
> 
> > Hacky but nice! For thread safety how about:
> > 
> > &nbsp;&nbsp;class Object  
> > &nbsp;&nbsp;&nbsp;&nbsp;def arg\_instance\_eval( \*args, &block )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = class \<\< self; self; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meth = "\_\_m\_#{Thread.current.object\_id}\_#{rand 666}"
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [19 September 2005 16:21 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/13 "2005-09-19T16:21:37Z")

</div>

\<snip\>

what i think to be the final version:

&nbsp;&nbsp;&nbsp;class Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def evaluate(\*a, &b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret, sent = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m = "\_\_\_\_evaluate\_\_\_\_#{ Thread::current.object\_id }\_\_\_\_#{ rand 666 }\_\_\_\_#{ rand 42 }\_\_\_\_"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = Class === self ? self : self::class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval{ define\_method m, &b }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret = send(sent = m, \*a)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval{ remove\_method m }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ensure  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if sent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;b = lambda{|x| p [x, y, self]}

&nbsp;&nbsp;&nbsp;c = C::new  
&nbsp;&nbsp;&nbsp;x = 42  
&nbsp;&nbsp;&nbsp;c.evaluate(x, &b) #=\> [42, 42, #\<C:0xb7f2f5c0\>]

-a

> **···**
>
> On Tue, 20 Sep 2005, Robert Klemme wrote:
> 
> > Hacky but nice! For thread safety how about:
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [19 September 2005 16:06 UTC](https://rubytalk.org/t/help-with-tricky-proc-binding-issue/20924/14 "2005-09-19T16:06:37Z")

</div>

proc.bound\_to(object), perhaps.

martin

> **···**
>
> Florian Groß \<florgro@gmail.com\> wrote:
> 
> > Procs are currently immutable and I'd assume that the source code  
> > assumes that they can not be changed in several places. It's less risky  
> > to return a new object with a changed self context, IMHO.
