Class: Binding

Class | Binding
In: | lib/active_support/binding_of_caller.rb
Parent: | Object

### Methods

of_caller

### Public Class methods

of_caller() {|result| ...}

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.

[Validate]