[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Easy way to use "self" in Proc

Leon Bogaert

5/16/2008 11:00:00 PM

Hi all,

I have this piece of code: http://pastie.caboo...
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
--
Posted via http://www.ruby-....

5 Answers

Daniel Finnie

5/16/2008 11:15:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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@(irb):1>
>> "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@(irb):5>
>> [1, 2, 3].instance_eval &t
=> 3
>> "Furry bunny".instance_eval &t
=> 11

Dan


On Fri, May 16, 2008 at 6:59 PM, Leon Bogaert <leon@tim-online.nl> wrote:

> Hi all,
>
> I have this piece of code: http://pastie.caboo...
> 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
> --
> Posted via http://www.ruby-....
>
>

Joel VanderWerf

5/16/2008 11:18:00 PM

0

Leon Bogaert wrote:
> Hi all,
>
> I have this piece of code: http://pastie.caboo...
> 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?

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)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Leon Bogaert

5/16/2008 11:39:00 PM

0

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:
http://www.bofh.org.uk/articles/2007/08/16/a-cunning-evil-trick...
--
Posted via http://www.ruby-....

Leon Bogaert

5/16/2008 11:44:00 PM

0

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.
--
Posted via http://www.ruby-....

Joel VanderWerf

5/17/2008 8:20:00 PM

0

Leon Bogaert 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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407