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?
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.