Jump to content

Ruby Programming/Standard Library/Tracer

From Wikibooks, open books for an open world

Tracer

[edit | edit source]

Ruby's built-in "tracer.rb" file shows you method by method which lines are called. To use it:

ruby -rtracer script_name.rb

for example, the following code:

class A
 def setup
  @instvar = 1
 end
 def go
  var = 2
  4.times do |@instvar|
   @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
 end
end
instance = A.new
instance.setup
instance.go

produces the following result:

C:\dev\digitalarchive_trunk>ruby -rtracer test.rb
#0:test.rb:1::-: class A
#0:test.rb:1:Class:>: class A
#0:test.rb:1:Class:<: class A
#0:test.rb:1::C: class A
#0:test.rb:2::-:   def setup
#0:test.rb:2:Module:>:   def setup
#0:test.rb:2:Module:<:   def setup
#0:test.rb:5::-:   def go
#0:test.rb:5:Module:>:   def go
#0:test.rb:5:Module:<:   def go
#0:test.rb:1::E: class A
#0:test.rb:11::-:  instance = A.new
#0:test.rb:11:Class:>:  instance = A.new
#0:test.rb:11:Object:>:  instance = A.new
#0:test.rb:11:Object:<:  instance = A.new
#0:test.rb:11:Class:<:  instance = A.new
#0:test.rb:12::-:  instance.setup
#0:test.rb:2:A:>:   def setup
#0:test.rb:3:A:-:    @instvar = 1`
#0:test.rb:4:A:<:   end`
#0:test.rb:13::-:  instance.go
#0:test.rb:5:A:>:   def go
#0:test.rb:6:A:-:    var = 2
#0:test.rb:7:A:-:    4.times do |@instvar|
#0:test.rb:7:Integer:>:    4.times do |@instvar|
#0:test.rb:8:A:-:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:>:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:<:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:A:-:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:>:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:<:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:A:-:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:>:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:<:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:A:-:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:>:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:8:Fixnum:<:     @instvar=@instvar*var end # <font color="#007700">#=>0,2,4,6 last assignment of @instvar is 6</font>
#0:test.rb:7:Integer:<:    4.times do |@instvar|
#0:test.rb:6:A:<:    var = 2