# module BindingOfCaller::BindingExtensions

### Public Instance Methods

callers() click to toggle source

Return bindings for all caller frames. @return [Array<Binding>]

    # File lib/binding_of_caller/mri2.rb, line 18
    def callers
      ary = []

      RubyVM::DebugInspector.open do |i|
        n = 0
        loop do
          begin
            b = i.frame_binding(n)
          rescue ArgumentError
            break
          end

          if b
            b.instance_variable_set(:@iseq, i.frame_iseq(n))
            ary << b
          end

          n += 1
        end
      end

      ary.drop(1)
    end

frame_count() click to toggle source

Number of parent frames available at the point of call. @return [Fixnum]

    # File lib/binding_of_caller/mri2.rb, line 44
    def frame_count
      callers.size - 1
    end

frame_description() click to toggle source

The description of the frame. @return [String]

    # File lib/binding_of_caller/mri2.rb, line 60
    def frame_description
      return nil if !@iseq
      @frame_description ||= @iseq.label
    end

frame_type() click to toggle source

The type of the frame. @return [Symbol]

    # File lib/binding_of_caller/mri2.rb, line 50
    def frame_type
      return nil if !@iseq

      # apparently the 9th element of the iseq array holds the frame type
      # ...not sure how reliable this is.
      @frame_type ||= @iseq.to_a[9]
    end

of_caller(n) click to toggle source

Retrieve the binding of the nth caller of the current frame. @return [Binding]

    # File lib/binding_of_caller/mri2.rb, line 7
    def of_caller(n)
      c = callers.drop(1)
      if n > (c.size - 1)
        raise "No such frame, gone beyond end of stack!"
      else
        c[n]
      end
    end