class Binding

Public Instance Methods

[]( x ) click to toggle source

Returns the value of some variable.

a = 2
binding["a"]  #=> 2
# File lib/core/facets/binding/op_get.rb, line 8
def []( x )
  eval( x.to_s )
end
[]=( l, v ) click to toggle source

Set the value of a local variable.

binding["a"] = 4
a  #=> 4

@deprecated No longer wortks in Ruby 1.9+.

@see Binding#with for an alternative.

# File lib/core/facets/binding/op_get.rb, line 20
def []=( l, v )
  eval( "lambda {|v| #{l} = v}").call( v )
end
__DIR__() click to toggle source

Return the directory of the file in which the binding was created.

# File lib/core/facets/binding/caller.rb, line 25
def __DIR__  
  File.dirname(self.__FILE__)
end
__FILE__() click to toggle source

Returns file name in which the binding was created.

# File lib/core/facets/binding/caller.rb, line 19
def __FILE__
  Kernel.eval("__FILE__", self)
end
__LINE__() click to toggle source

Return the line number on which the binding was created.

# File lib/core/facets/binding/caller.rb, line 13
def __LINE__
  Kernel.eval("__LINE__", self)
end
__callee__() click to toggle source

Retreive the current running method.

# File lib/core/facets/binding/caller.rb, line 37
def __callee__
  Kernel.eval("__callee__", self)
end
__method__() click to toggle source

Retreive the current running method.

# File lib/core/facets/binding/caller.rb, line 31
def __method__
  Kernel.eval("__method__", self)
end
block_exec(*args, &block) click to toggle source

Allows the evaluation of blocks by a Binding in the same way that strings can be evaluated.

x = 5
$my_binding = binding

class Test  # just here to provide a scope gate
  $my_binding.block_exec { x }
end

# => 5

NOTE: The implementation of this method uses a method_missing trick.

Consequently it is a bit of a hack.
# File lib/standard/facets/binding/block_exec.rb, line 18
def block_exec(*args, &block)
  BlockEnvironment.new(self, *args, &block).call
end
call_stack(level=1)
Alias for: callstack
caller( skip=0 ) click to toggle source

Returns the call stack, same format as Kernel#caller()

# File lib/core/facets/binding/caller.rb, line 7
def caller( skip=0 )
  eval("caller(#{skip})")
end
callstack(level=1) click to toggle source

Returns the call stack, in array format.

# File lib/core/facets/kernel/callstack.rb, line 50
def callstack(level=1)
  eval( "callstack( #{level} )" )
end
Also aliased as: call_stack
defined?(x) click to toggle source

Returns the nature of something within the context of the binding. Returns nil if that thing is not defined.

# File lib/core/facets/binding/defined.rb, line 5
def defined?(x)
  eval("defined? #{x}")
end
self() click to toggle source

Returns self of the binding’s context.

# File lib/core/facets/binding/self.rb, line 8
def self
  eval('self')
end
with(_local_variables, &_yields) click to toggle source

Returns a new binding with local varaibles set.

CREDIT: Trans

# File lib/core/facets/binding/with.rb, line 7
def with(_local_variables, &_yields)
  eval("lambda{ |#{_local_variables.keys.join(',')},&yields| binding }").call(*_local_variables.values, &_yields)
end