Class Binding
In: lib/facets/core/binding/call_stack.rb
lib/facets/core/binding/defined.rb
lib/facets/core/binding/local_variables.rb
lib/facets/core/binding/method_name.rb
lib/facets/core/binding/caller.rb
lib/facets/core/binding/__LINE__.rb
lib/facets/core/binding/called.rb
lib/facets/core/binding/eval.rb
lib/facets/core/binding/op_fetch.rb
lib/facets/core/binding/self.rb
lib/facets/core/binding/op_store.rb
lib/facets/core/binding/self/of_caller.rb
Parent: Object

rescue LoadError

  class Continuation; end # :nodoc: # for RDoc
  def Continuation.create(*args, &block) # :nodoc:
    cc = nil; result = callcc {|c| cc = c; block.call(cc) if block and args.empty?}
    result ||= args
    return *[cc, *result]
  end

end

Methods

[]   []=   __DIR__   __FILE__   __LINE__   call_stack   called   caller   defined?   eval   local_variables   methodname   of_caller   self  

Public Class methods

This method returns the binding of the method that called your method. It will raise an Exception when you‘re not inside a method.

It‘s used like this:

  def inc_counter(amount = 1)
    Binding.of_caller do |binding|
      # Create a lambda that will increase the variable 'counter'
      # in the caller of this method when called.
      inc = eval("lambda { |arg| counter += arg }", binding)
      # We can refer to amount from inside this block safely.
      inc.call(amount)
    end
    # No other statements can go here. Put them inside the block.
  end
  counter = 0
  2.times { inc_counter }
  counter # => 2

Binding.of_caller must be the last statement in the method. This means that you will have to put everything you want to do after the call to Binding.of_caller into the block of it. This should be no problem however, because Ruby has closures. If you don‘t do this an Exception will be raised. Because of the way that Binding.of_caller is implemented it has to be done this way.

++Binding-of-Caller, Copyright (c) 2003 Florian Gross++

Public Instance methods

Returns the value of some variable.

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

Set the value of a local variable.

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

return the directory of the file

returns file name

returns line number

Returns the call stack, in array format.

Retreive the current running method.

  def tester; p called; end
  tester  #=> :tester

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

Returns the nature of something, nil if that thing is not defined.

Evaluate a Ruby source code string (or block) in the binding context

Returns the local variables defined in the binding context

  a = 2
  binding.local_variables  #=> ["a"]

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"

Returns the self in the binding context.

[Validate]