Module: BindingOfCaller::BindingExtensions

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

Instance Method Summary collapse

Instance Method Details

#callersArray<Binding>

Return bindings for all caller frames.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/binding_of_caller.rb', line 39

def callers
  ary = []
  n = 0
  loop do
    begin
      ary << Binding.of_caller(n)
    rescue
      break
    end
    n += 1
  end
  ary.drop_while do |v|
    !(v.frame_type == :method && v.eval("__method__") == :callers)
  end.drop(1)
end

#frame_countFixnum

Number of parent frames available at the point of call.

Returns:

  • (Fixnum)


57
58
59
# File 'lib/binding_of_caller.rb', line 57

def frame_count
  callers.size - 1
end

#frame_descriptionString

The description of the frame.

Returns:

  • (String)


33
34
35
# File 'lib/binding_of_caller.rb', line 33

def frame_description
  @frame_description
end

#frame_typeSymbol

The type of the frame.

Returns:

  • (Symbol)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/binding_of_caller.rb', line 63

def frame_type
  case self.variables.method..to_a.first.to_s
  when /block/
    :block
  when /eval/
    :eval
  else
    if frame_description =~ /__(?:class|module)_init__/
      :class
    else
      :method
    end
  end
end

#of_caller(n) ⇒ Binding

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

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/binding_of_caller.rb', line 13

def of_caller(n)
  bt = Rubinius::VM.backtrace(1 + n, true).first

  b = Binding.setup(
                bt.variables,
                bt.variables.method,
                bt.static_scope,
                bt.variables.self,
                bt
                )

  b.instance_variable_set(:@frame_description, bt.describe)

  b
  rescue
    raise RuntimeError, "Invalid frame, gone beyond end of stack!"
end