From: Phrogz Date: 2010-01-13T07:45:04+09:00 Subject: Re: instance_eval with local variable On Jan 12, 1:19 pm, Brian Candler wrote: > I believe your lambda is bound to the @who in the scope where it was > defined, you can't dynamically rebind it. Nope: >cat tmp.rb @who = "main" proc = ->{ p @who } proc[] bar = Object.new; bar.instance_variable_set :@who, "bar" bar.instance_eval( &proc ) >ruby -v tmp.rb ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32] "main" "bar" instance_eval can rebind the proc, but doesn't let you pass arguments to it. > I think that what you ask for would lead to very surprising behaviour, > especially when using blocks: > >    @count = 0 >    something.whatever { @who += 1 } Sure, the concept of a closure is great and 99% of the time should be preserved. I wasn't asking for my edge-case behavior to be the norm, but rather trying to figure if it's possible at all. > > 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) Interesting. Slight downside is the requirement that all plug-ins have unique method names. Or...hrm, perhaps some insanity involving define_method on the receiving end of the proc. Thanks for the suggestion, anyhow.