On Sun, Nov 13, 2011 at 8:30 PM, Intransition [email protected]
wrote:
b.eval('a') #=> 1
Note I had to explicitly define this in Binding to avoid the private
method
invocation.
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 ': no block given (yield) (LocalJumpError)
On Sunday, November 13, 2011 10:16:37 PM UTC-5, Josh C. wrote:
Note I had to explicitly define this in Binding to avoid the private method
invocation.
Sorry, I should have clarified that. Yes, that is the intent.
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
On Mon, Nov 14, 2011 at 10:41 AM, Intransition [email protected]
wrote:
(class << self; self; end).class_eval(code)
Not sure what you mean, it seems to work for me:
class Binding
def with(_hash={}, &_yield)
singleton_class.class_eval(<<-CODE)
define_method :_with do |#{_hash.keys.join(’,’)}|
binding
end
CODE
bnd = _with(*_hash.values, &_yield)
singleton_class.class_eval { remove_method :_with }
bnd
end
end
RUBY_VERSION # => “1.9.3”
binding.with(:a => 10) { |x| x + x }.eval(‘yield a + 5’) # => 30