Using Ruby's built-in tracer

require 'tracer'

class Fun

  def breathe()
    puts 'breathing in'
  end

  def moo(s)
    breathe
    puts 'you said moo ' + s
  end

end

Tracer.add_filter do |event, file, line, id, binding, klass, *rest|
  if klass.to_s == 'Fun' then
    puts 'klass : ' + klass.to_s
    #puts 'file :' + file.inspect
    puts 'event :'  + event.inspect
    #puts 'line :' + line.inspect
    puts 'id : ' + id.inspect
    #puts 'binding : ' + binding.inspect
    #puts 'rest : '  + rest.inspect
    puts '--------------'
  end
end

Tracer.on

fun = Fun.new
fun.moo ' x 2'

output:

klass : Fun
even

t :"call"

id : :moo
--------------
klass : Fun
event :"line"
id : :moo
--------------
klass : Fun
event :"call"
id : :breathe
--------------
klass : Fun
event :"line"
id : :breathe
--------------
breathing in
klass : Fun
event :"return"
id : :breathe
--------------
klass : Fun
event :"line"
id : :moo
--------------
you said moo  x 2
klass : Fun
event :"return"
id : :moo
--------------


Resources

Tags:
Source:
1322hrs.txt
Published:
25-01-2013 13:22