Class: Binding
- Defined in:
- lib/core/facets/binding/eval.rb,
lib/core/facets/binding/self.rb,
lib/core/facets/binding/vars.rb,
lib/core/facets/binding/cflow.rb,
lib/core/facets/binding/defined.rb
Instance Method Summary collapse
-
#[](x) ⇒ Object
Returns the value of some variable.
-
#[]=(l, v) ⇒ Object
Set the value of a local variable.
-
#__DIR__ ⇒ Object
Return the directory of the file.
-
#__FILE__ ⇒ Object
Returns file name.
-
#__LINE__ ⇒ Object
Returns line number.
-
#call_stack(level = 1) ⇒ Object
Returns the call stack, in array format.
-
#called ⇒ Object
Retreive the current running method.
-
#caller(skip = 0) ⇒ Object
Returns the call stack, same format as Kernel#caller().
-
#defined?(x) ⇒ Boolean
Returns the nature of something within the context of the binding.
-
#eval(str) ⇒ Object
Evaluate a Ruby source code string (or block) in the binding context.
-
#local_variables ⇒ Object
Returns the local variables defined in the binding context.
-
#methodname ⇒ Object
There is a lot of debate on what to call this.
-
#self ⇒ Object
Returns self of the binding context.
Instance Method Details
#[](x) ⇒ Object
Returns the value of some variable.
a = 2
binding["a"] #=> 2
28 29 30 |
# File 'lib/core/facets/binding/vars.rb', line 28 def []( x ) eval( x.to_s ) end |
#[]=(l, v) ⇒ Object
Set the value of a local variable.
binding["a"] = 4
a #=> 4
37 38 39 |
# File 'lib/core/facets/binding/vars.rb', line 37 def []=( l, v ) eval( "lambda {|v| #{l} = v}").call( v ) end |
#__DIR__ ⇒ Object
Return the directory of the file.
27 28 29 |
# File 'lib/core/facets/binding/cflow.rb', line 27 def __DIR__ eval("File.dirname(__FILE__)") end |
#__FILE__ ⇒ Object
Returns file name.
21 22 23 |
# File 'lib/core/facets/binding/cflow.rb', line 21 def __FILE__ eval("__FILE__") end |
#__LINE__ ⇒ Object
Returns line number.
15 16 17 |
# File 'lib/core/facets/binding/cflow.rb', line 15 def __LINE__ eval("__LINE__") end |
#call_stack(level = 1) ⇒ Object
Returns the call stack, in array format.
33 34 35 |
# File 'lib/core/facets/binding/cflow.rb', line 33 def call_stack(level=1) eval( "call_stack( #{level} )" ) end |
#called ⇒ Object
Retreive the current running method.
def tester; p called; end
tester #=> :tester
43 44 45 46 |
# File 'lib/core/facets/binding/cflow.rb', line 43 def called name = /\`([^\']+)\'/.match(caller(1).first)[1] return name.to_sym end |
#caller(skip = 0) ⇒ Object
Returns the call stack, same format as Kernel#caller()
50 51 52 |
# File 'lib/core/facets/binding/cflow.rb', line 50 def caller( skip=0 ) eval("caller(#{skip})") end |
#defined?(x) ⇒ Boolean
Returns the nature of something within the context of the binding. Returns nil if that thing is not defined.
6 7 8 |
# File 'lib/core/facets/binding/defined.rb', line 6 def defined?(x) eval("defined? #{x}") end |
#eval(str) ⇒ Object
Evaluate a Ruby source code string (or block) in the binding context.
7 8 9 10 11 12 13 |
# File 'lib/core/facets/binding/eval.rb', line 7 def eval(str) #='', &blk ) #if block_given? # Kernel.eval( self, &blk ) #elsif str Kernel.eval(str, self) #end end |
#local_variables ⇒ Object
Returns the local variables defined in the binding context
a = 2
binding.local_variables #=> ["a"]
19 20 21 |
# File 'lib/core/facets/binding/vars.rb', line 19 def local_variables() eval("local_variables") end |
#methodname ⇒ Object
There is a lot of debate on what to call this. method_name differs from #called only by the fact that it returns a string, rather then a symbol.
def tester; p methodname; end
tester #=> "tester"
62 63 64 65 |
# File 'lib/core/facets/binding/cflow.rb', line 62 def methodname name = /\`([^\']+)\'/.match(caller(1).first)[1] return name end |
#self ⇒ Object
Returns self of the binding context.
5 6 7 8 |
# File 'lib/core/facets/binding/self.rb', line 5 def self() @self ||= eval("self") @self end |