class Binding

class Hash

#
def to_hash
  dup
end unless method_defined?(:to_hash)

#
def rekey(&block)
  h = {}
  if block
    each{|k,v| h[block[k]] = v }
  else
    each{|k,v| h[k.to_sym] = v }
  end
  h
end unless method_defined?(:rekey)

end

Public Instance Methods

itself() click to toggle source
# File lib/malt/core_ext.rb, line 36
def itself
  eval('self')
end
to_binding() click to toggle source

Conversion for bindings.

@todo Is there any way to integrate the optional data and block?

# File lib/malt/core_ext.rb, line 31
def to_binding
  self
end
with(_hash, &_yield) click to toggle source

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).

# File lib/malt/core_ext.rb, line 45
  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
  end