Module: BindingOfCaller::BindingExtensions

Included in:
Binding, Binding
Defined in:
lib/binding_of_caller/mri2.rb,
lib/binding_of_caller/rubinius.rb

Instance Method Summary collapse

Instance Method Details

#callersArray<Binding>

Return bindings for all caller frames.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) 

        if b
          iseq = i.frame_iseq(n) 
          b.instance_variable_set(:@iseq, iseq)
          ary << b
        end
      rescue ArgumentError
        break
      end
      n += 1
    end
  end
  
  ary.drop(1)
end

#frame_countFixnum

Number of parent frames available at the point of call.

Returns:

  • (Fixnum)


44
45
46
# File 'lib/binding_of_caller/mri2.rb', line 44

def frame_count
  callers.size - 1
end

#frame_descriptionString

The description of the frame.

Returns:

  • (String)


60
61
62
63
# File 'lib/binding_of_caller/mri2.rb', line 60

def frame_description
  return nil if !@iseq
  @frame_description ||= @iseq.label
end

#frame_typeSymbol

The type of the frame.

Returns:

  • (Symbol)


50
51
52
53
54
55
56
# 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) ⇒ Binding

Retrieve the binding of the nth caller of the current frame.

Returns:



7
8
9
10
11
12
13
14
# 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