Prev | Current Page 77 | Next

Brad Ediger

"Advanced Rails"

8.4.rb:17"]
baz
However, this method is clunky. There is very little information that we can use programmatically;
this is mainly for informational use. A more flexible stack trace mechanism
comes from Mauricio Fern??ndez??™s call_stack library,* which hooks entry and exit
of every method, providing that information at the request of the global call_stack
method. This library was developed because Ruby 1.8.5 broke the old implementation
of Binding.of_caller (which relied on a bug in 1.8.4). However, call_stack works on
1.8.4 as well. We get much more information through call_stack:
#!/usr/local/bin/ruby
require 'rubygems'
require 'call_stack'
require 'pp'
class Test
def foo
bar
end
def bar
pp call_stack(-1)
"baz"
end
end
call_stack_on
puts Test.new.foo
call_stack_off
The call_stack_on and call_stack_off functions add and remove the hook functions
that keep track of function execution, so you must call call_stack_on before
starting to capture frames. The call_stack function yields an array of stack frames;
each frame is an array containing [class_name, method_name, filename, line, binding,
language]. This code prints:
* http://eigenclass.org/hiki.rb?call_stack
52 | Chapter 2: ActiveSupport and RailTies
[[:unknown, :unknown, "./call_stack1.8.5.rb", 18, nil, nil],
[Test, :foo, "./call_stack1.8.5.rb", 9, #, :Ruby],
[Test, :bar, ".


Pages:
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89