Posted by & filed under Ruby.

TracePoint?

Ruby 2 gives us a brand new class to play with. Ladies and gentlemen, please allow me to introduce TracePoint.

 

What does it do?

TracePoint allows you to inspect the inner workings of your application’s execution via a super groovy API. You can make it do all kinds of useful and useless things. For example, how about running some code that prints “WOOOOO!!” to the console every time an exception gets raised? Let’s do it:

 

trace = TracePoint.new(:raise) do |t|
puts "WOOOOO!!"
end

 

This defines our new TracePoint. Notice that passing it :raise tells our TracePoint to run the code in it’s block each time an exception gets raised.

 

trace.enable

 

TracePoints get initiated as disabled, so we have to enable the thing before using it. Now we are all set to invoke it by causing an exception:

 

require "file_that_doesnt_exist"
WOOOOO!!
[36, :raise, #<LoadError: cannot load such file -- file_that_doesnt_exist>]

 

See, we invoked a LoadError by trying to require a file that doesn’t exist. Our TracePoint is looking out for exceptions so when it gets invoked, the code in our TracePoint block gets run and puts the “WOOOOO!!” to the console. GROOOOVY!! In the real world, you could use it to log exceptions somewhere weird or ping you an email.

But TracePoint is not limited to just listening out for exceptions, oh no. We can listen for all kinds of events. How about firing some code whenever a method returns? Watch…

 

trace = TracePoint.new(:return) do |t|
puts "#{t.method_id} has just done its thing."
end

 

The TracePoint block lets you investigate which method is being worked upon by accessing the method_id property. Handy! Ok, let’s fire up the TracePoint:

 

trace.enable #=>

evaluate has just done its thing.
conf has just done its thing.
CurrentContext has just done its thing.
evaluate has just done its thing.
set_last_value has just done its thing.
evaluate has just done its thing.
inspect_value has just done its thing.
inspect_last_value has just done its thing.

 

Oh Wow! Just look at that output! Just by enabling the TracePoint we can see Ruby firing and returning from all kinds of methods under the covers! Pretty good! Pretty good! Hopefully now you can see how TracePoint gives you handle on the execution path of Ruby. To wrap up, here is a list of the events you can get your TracePoint to listen out for:

 

:line | execute code on a new line
:class | start a class or module definition
:end | finish a class or module definition
:call | call a Ruby method
:return | return from a Ruby method
:c_call | call a C-language routine
:c_return | return from a C-language routine
:raise | raise an exception
:b_call | event hook at block entry
:b_return | event hook at block ending
:thread_begin | event hook at thread beginning
:thread_end | event hook at thread ending

 

Final note: If you want to learn more about Ruby 2 and have a better understanding of how it all works in detail, why not sign up for Codesonic? It drills you on every method and class of Ruby so that you learn it forever. Check it out at www.codesonic.com

Trackbacks/Pingbacks

  1.  Ruby 2.0 Released: Let Tracing Begin! | AppNeta

Leave a Reply

  • (will not be published)