--0016364d237f12d26304b1a94c82 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Nov 13, 2011 at 8:30 PM, Intransition <transfire / gmail.com> wrote: > Binding#with > > def with(_local_variables) > eval("Proc.new{ |#{_local_variables.keys.join(',')}| binding > }").call(*_local_variables.values) > end > > Try it: > > b inding.with(:a) > b.eval('a') # 1 > > Note I had to explicitly define this in Binding to avoid the private method invocation. > Now, it there a way to add a "yield" to the binding? I tried: > > def with(_local_variables, &_yields) > eval("lambda{ |#{_local_variables.keys.join(',')},&yields| binding > }").call(*_local_variables.values, &_yields) > end > > b inding.with{ |x| x+x} > b.eval('yields[1]') # 2 > > Note that this didn't work for me unless I passed a hash to Binding#with, because otherwise the syntax it generates looks like `lambda{ |,&yields| binding }` which is a syntax error > but > > b.eval('yield(1)') # Error > > Seems doubtful, b/c I think `yield` only exists in methods, not in procs (this is just based on behaviour I've experienced, not on an implementation level knowledge) def l() yield 1 end method(:l).call { |x| x + x } # 2 vs lambda { yield 1 }.call { |x| x + x } # ~> -:1:in `block in <main>': no block given (yield) (LocalJumpError) --0016364d237f12d26304b1a94c82--