Easy way to use "self" in Proc

L

Leon Bogaert

Hi all,

I have this piece of code: 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
 
D

Daniel Finnie

[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: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 }
=> # said:
[1, 2, 3].instance_eval &t => 3
"Furry bunny".instance_eval &t
=> 11

Dan
 
J

Joel VanderWerf

Leon said:
Hi all,

I have this piece of code: 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?

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

Leon Bogaert

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

Joel VanderWerf

Leon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top