Class: Binding
- Defined in:
- lib/core/facets/binding/self.rb,
lib/core/facets/binding/with.rb,
lib/core/facets/binding/caller.rb,
lib/core/facets/binding/op_get.rb,
lib/core/facets/binding/defined.rb,
lib/core/facets/kernel/callstack.rb,
lib/standard/facets/binding/block_exec.rb
Defined Under Namespace
Classes: BlockEnvironment
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the value of a local variable.
-
#[]=(name, value) ⇒ Object
Set the value of a local variable.
-
#__callee__ ⇒ Object
Retreive the current running method.
-
#__DIR__ ⇒ Object
Return the directory of the file in which the binding was created.
-
#__FILE__ ⇒ Object
Returns file name in which the binding was created.
-
#__LINE__ ⇒ Object
Return the line number on which the binding was created.
-
#__method__ ⇒ Object
Retreive the current running method.
-
#block_exec(*args, &block) ⇒ Object
Allows the evaluation of blocks by a Binding in the same way that strings can be evaluated.
-
#caller(skip = 0) ⇒ Object
Returns the call stack, same format as Kernel#caller().
-
#caller_locations(skip = 0) ⇒ Object
Returns the call stack, same format as Kernel#caller_locations().
-
#callstack(level = 1) ⇒ Object
(also: #call_stack)
Returns the call stack, in array format.
-
#defined?(x) ⇒ Boolean
Returns the nature of something within the context of the binding.
-
#self ⇒ Object
deprecated
Deprecated.
Use Binding#receiver instead (built-in since Ruby 2.6).
-
#with(_local_variables, &_yields) ⇒ Object
Returns a new binding with local varaibles set.
Instance Method Details
#[](name) ⇒ Object
Returns the value of a local variable.
a = 2
binding[:a] #=> 2
8 9 10 |
# File 'lib/core/facets/binding/op_get.rb', line 8 def [](name) local_variable_get(name.to_sym) end |
#[]=(name, value) ⇒ Object
Set the value of a local variable.
binding[:a] = 4
a #=> 4
17 18 19 |
# File 'lib/core/facets/binding/op_get.rb', line 17 def []=(name, value) local_variable_set(name.to_sym, value) end |
#__callee__ ⇒ Object
Retreive the current running method.
43 44 45 |
# File 'lib/core/facets/binding/caller.rb', line 43 def __callee__ Kernel.eval("__callee__", self) end |
#__DIR__ ⇒ Object
Return the directory of the file in which the binding was created.
31 32 33 |
# File 'lib/core/facets/binding/caller.rb', line 31 def __DIR__ File.dirname(self.__FILE__) end |
#__FILE__ ⇒ Object
Returns file name in which the binding was created.
25 26 27 |
# File 'lib/core/facets/binding/caller.rb', line 25 def __FILE__ self.source_location[0] end |
#__LINE__ ⇒ Object
Return the line number on which the binding was created.
19 20 21 |
# File 'lib/core/facets/binding/caller.rb', line 19 def __LINE__ self.source_location[1] end |
#__method__ ⇒ Object
Retreive the current running method.
37 38 39 |
# File 'lib/core/facets/binding/caller.rb', line 37 def __method__ Kernel.eval("__method__", self) end |
#block_exec(*args, &block) ⇒ Object
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.
18 19 20 |
# File 'lib/standard/facets/binding/block_exec.rb', line 18 def block_exec(*args, &block) BlockEnvironment.new(self, *args, &block).call end |
#caller(skip = 0) ⇒ Object
Returns the call stack, same format as Kernel#caller()
7 8 9 |
# File 'lib/core/facets/binding/caller.rb', line 7 def caller( skip=0 ) eval("caller(#{skip})") end |
#caller_locations(skip = 0) ⇒ Object
Returns the call stack, same format as Kernel#caller_locations()
13 14 15 |
# File 'lib/core/facets/binding/caller.rb', line 13 def caller_locations( skip=0 ) eval("caller_locations(#{skip})") end |
#callstack(level = 1) ⇒ Object Also known as: call_stack
Returns the call stack, in array format.
32 33 34 |
# File 'lib/core/facets/kernel/callstack.rb', line 32 def callstack(level=1) eval( "callstack( #{level} )" ) end |
#defined?(x) ⇒ Boolean
Returns the nature of something within the context of the binding. Returns nil if that thing is not defined.
5 6 7 |
# File 'lib/core/facets/binding/defined.rb', line 5 def defined?(x) eval("defined? #{x}") end |
#self ⇒ Object
Use Binding#receiver instead (built-in since Ruby 2.6).
4 5 6 7 |
# File 'lib/core/facets/binding/self.rb', line 4 def self warn "Binding#self is deprecated. Use Binding#receiver instead.", uplevel: 1 receiver end |
#with(_local_variables, &_yields) ⇒ Object
Returns a new binding with local varaibles set.
CREDIT: Trans
7 8 9 |
# 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 |