Module: BindingOfCaller::BindingExtensions

Included in:
Binding, Binding
Defined in:
lib/binding_of_caller/mri2.rb,
lib/binding_of_caller/rubinius.rb,
lib/binding_of_caller/jruby_interpreted.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
# File 'lib/binding_of_caller/mri2.rb', line 18

def callers
  ary = []

  RubyVM::DebugInspector.open do |dc|
    locs = dc.backtrace_locations

    locs.size.times do |i|
      b = dc.frame_binding(i)
      if b
        b.instance_variable_set(:@iseq, dc.frame_iseq(i))
        ary << b
      end
    end
  end

  ary.drop(1)
end

#frame_countFixnum

Number of parent frames available at the point of call.

Returns:

  • (Fixnum)


38
39
40
# File 'lib/binding_of_caller/mri2.rb', line 38

def frame_count
  callers.size - 1
end

#frame_descriptionString

The description of the frame.

Returns:

  • (String)


54
55
56
57
# File 'lib/binding_of_caller/mri2.rb', line 54

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

#frame_typeSymbol

The type of the frame.

Returns:

  • (Symbol)


44
45
46
47
48
49
50
# File 'lib/binding_of_caller/mri2.rb', line 44

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(index = 1) ⇒ Binding

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

Returns:

Raises:

  • (RuntimeError)


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