From: Intransition Date: 2011-11-15T01:41:07+09:00 Subject: Re: Binding#with ------=_Part_145_31948876.1321288863978 Content-Type: multipart/alternative; boundary="----=_Part_146_26743045.1321288863979" ------=_Part_146_26743045.1321288863979 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Well, this seems to work: # def with(_hash, &_yield) _hash = (_hash || {}).to_hash code = <<-END define_method(:_with) do |#{_hash.keys.join(',')}| binding end END (class << self; self; end).class_eval(code) b = _with(*_hash.values, &_yield) (class << self; self; end).class_eval{ remove_method(:_with) } return b #eval("Proc.new{ |#{_hash.keys.join(',')}| binding }").call(*_hash.values) end But alas, now I am loosing the context of original binding. Why is that? I thought define_method kept closure. ------=_Part_146_26743045.1321288863979 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
Well, this seems to work:

  #
  def with(_hash, &_yield)
    _hash = (_hash || {}).to_hash
    code = <<-END
      define_method(:_with) do |#{_hash.keys.join(',')}|
        binding
      end
    END
    (class << self; self; end).class_eval(code)
    b = _with(*_hash.values, &_yield)
    (class << self; self; end).class_eval{ remove_method(:_with) }
    return b
    #eval("Proc.new{ |#{_hash.keys.join(',')}| binding }").call(*_hash.values)
  end

But alas, now I am loosing the context of original binding. Why is that? I thought define_method kept closure.

------=_Part_146_26743045.1321288863979-- ------=_Part_145_31948876.1321288863978--