Class: Binding

Inherits:
Object show all
Defined in:
lib/malt/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#itselfObject



36
37
38
# File 'lib/malt/core_ext.rb', line 36

def itself
  eval('self')
end

#to_bindingObject

TODO:

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

Conversion for bindings.



31
32
33
# File 'lib/malt/core_ext.rb', line 31

def to_binding
  self
end

#with(_hash, &_yield) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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