# Easy way to use "self" in Proc

**URL:** <https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864>\
**Category:** Ruby\
**Created:** [May 17, 2008, 12:59am UTC](https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864 "2008-05-17T00:59:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 17, 2008, 12:59am UTC](https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864/1 "2008-05-17T00:59:41Z")

</div>

Hi all,

I have this piece of code: [http://pastie.caboo.se/198524](http://pastie.caboo.se/198524)  
I’m trying to use the “self” context of the instanciated class in the  
Proc. I’ve got it working now, but isn’t there a nicer way to use the  
Proc.new() without having to use the “|obj|” part?

So instead of:

t.code = Proc.new { |obj| obj.instance\_test() }

I want to use something like:  
t.code = Proc.new { self.instance\_test() }

I’ve tried some stuff but I can’t / don’t think I can get it working the  
second way. Does anyone have some bright ideas about solving this  
problem?

Thanks in advance!  
Leon

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 17, 2008, 1:15am UTC](https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864/2 "2008-05-17T01:15:34Z")

</div>

Hi,

It is possible. You need to tell the object to evaluate the proc with  
self  
as that object like so:

> > t = Proc.new { self.length }  
> > =\> #Proc:0xb77b7bd8@:1(irb)
> 
> > “Furry bunny”.instance\_eval &t \<-- note the ampersand. This means pass  
> > the proc as a block.  
> > =\> 11
> 
> > [1, 2, 3].instance\_eval &t  
> > =\> 3

Because self is an implicit receiver (that is, if no other reciever is  
specified, self is the receiver), you can leave off self entirely:

> > t = Proc.new { length }  
> > =\> #Proc:0xb77ad6ec@:5(irb)
> 
> > [1, 2, 3].instance\_eval &t  
> > =\> 3
> 
> > “Furry bunny”.instance\_eval &t  
> > =\> 11

Dan

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 17, 2008, 1:19am UTC](https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864/3 "2008-05-17T01:19:05Z")

</div>

Leon B. wrote:

> I want to use something like:  
> t.code = Proc.new { self.instance\_test() }
> 
> I’ve tried some stuff but I can’t / don’t think I can get it working the  
> second way. Does anyone have some bright ideas about solving this  
> problem?

Fortunately, it’s easy:

t.code = Proc.new { self.instance\_test() }

and instead of

t.code.call(obj)

you just do this

obj.instance\_eval(&t.code)

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 17, 2008, 1:38am UTC](https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864/4 "2008-05-17T01:38:42Z")

</div>

Wow, dudes! Thanks!

That’s simple… I’ll read your posts some more times to really  
understand what’s hapening 🙂  
'Cause this is like magic to me.

After googling on the solution I also found this blog:

> **[A cunning (evil?) trick with Ruby](https://bofh.org.uk/2007/08/16/a-cunning-evil-trick-with-ruby/)**
>
> One of the handy tools that Ruby makes available to us Domain Specific Pidgin builders is instance\_eval. With instance\_eval you can take a block that was created in one context, with all its lovely closed over local variables, and evaluate it in the...

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 17, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864/5 "2008-05-17T22:20:26Z")

</div>

Leon B. wrote:

> I thought I understood the block/proc thing. Guess not…
> 
> I thought when I proc is converted to a block, the code gets evaluated.  
> So (I thought) when Proc.call() is, euhm, called, the proc gets  
> converted to a block and is getting evaluated in that context.

The block keeps the original context (“lexical scoping”), including  
local vars, self, and constants:

class C  
class D; end  
def foo  
local = 1  
proc { [local, self, D] }  
end  
end

class D; end  
local = 2  
p [local, self, D]

p C.new.foo.call

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [May 17, 2008, 1:43am UTC](https://www.ruby-forum.com/t/easy-way-to-use-self-in-proc/137864/6 "2008-05-17T01:43:46Z")

</div>

I thought I understood the block/proc thing. Guess not…

I thought when I proc is converted to a block, the code gets evaluated.  
So (I thought) when Proc.call() is, euhm, called, the proc gets  
converted to a block and is getting evaluated in that context.
