Gavin Kistner wrote:
> I have a proc with arity 1:
> @who= "main"
> holla = lambda{ |str| p @who, str }
I believe your lambda is bound to the @who in the scope where it was
defined, you can't dynamically rebind it.
To demonstrate:
class A
def initialize
@x = 123
end
def run
fn = lambda { puts "@x = #{@x}" }
B.new(fn).run
end
end
class B
def initialize(fn)
@x = 456
@fn = fn
end
def run
@fn.call
end
end
A.new.run # => @x = 123
In other words, @who is not the same as
self.instance_variable_get(:@who), as far as I can see anyway.
I think that what you ask for would lead to very surprising behaviour,
especially when using blocks:
@count = 0
something.whatever { @who += 1 }
You wouldn't want @who ever to be bound to some other object further
down the call chain.
> For those interested in second-guessing my approach and coming up with
> a better way to achieve the same end goal
Perhaps rather than using lambdas, define methods on the object which
contains the instance variables you're interested in, then call those
methods (or use method(:name) to get a Method object, which you can pass
around pretty much like a lambda)
--
Posted via
http://www.ruby-forum.com/.