From: Intransition Date: 2011-11-22T21:24:12+09:00 Subject: Re: Binding#with ------=_Part_3_19832473.1321964649357 Content-Type: multipart/alternative; boundary="----=_Part_4_28210001.1321964649358" ------=_Part_4_28210001.1321964649358 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Wrapping head thoroughly round pole resulted in: # Create a new binding incorporating the current binding and # the given local settings hash and yield block. # # The yield code was neccessary b/c Ruby does not respect the use # of yield in a lambda (boo hiss). def with(_hash, &_yield) _hash = (_hash || {}).to_hash if _yield vars = eval('local_variables') vals = eval("[#{vars.join(',')}]") vars += _hash.keys vals += _hash.values code = <<-END def self.___with(#{vars.join(',')}) binding end method(:___with) END eval(code).call(*vals, &_yield) #_args = _hash.empty? ? '' : '|' + _hash.keys.join(',') + ',&y|' #lamb = eval("lambda{#{_args} binding}") #(class << self; self; end).send(:define_method, :__temp__, &lamb) #method(:__temp__).call(*_hash.values, &_yield) else _args = _hash.empty? ? '' : '|' + _hash.keys.join(',') + '|' eval("lambda{#{_args} binding}").call(*_hash.values) end Now, can it be thread safe? ------=_Part_4_28210001.1321964649358 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Wrapping head thoroughly round pole resulted in:
<= br>
  # Create a new binding incorporating the current bindi= ng and
  # the given local settings hash and yield block.
  #
  # The yield code was neccessary b/c Ruby = does not respect the use
  # of yield in a lambda (boo hiss)= . 
  def with(_hash, &_yield)
  &nbs= p; _hash =3D (_hash || {}).to_hash

    i= f _yield
      vars =3D eval('local_variables')
      vals =3D eval("[#{vars.join(',')}]")

      vars +=3D _hash.keys
  =     vals +=3D _hash.values

   =   code =3D <<-END
        def sel= f.___with(#{vars.join(',')})
          b= inding
        end
    &n= bsp;   method(:___with)
      END
&= nbsp;     eval(code).call(*vals, &_yield)

      #_args =3D _hash.empty? ? '' : '|' + _hash.keys.= join(',') + ',&y|'
      #lamb =3D eval("lambd= a{#{_args} binding}")
      #(class << self;= self; end).send(:define_method, :__temp__, &lamb)
  &nb= sp;   #method(:__temp__).call(*_hash.values, &_yield)
&n= bsp;   else
      _args =3D _hash.empty? ? ''= : '|' + _hash.keys.join(',') + '|'
      eval("la= mbda{#{_args} binding}").call(*_hash.values)
    end

Now, can it be thread safe?

------=_Part_4_28210001.1321964649358-- ------=_Part_3_19832473.1321964649357--