To debug simple Ruby scripts, just run your scripts with rdebug rather
than ruby. This is an extremely helpful way to examine the path Ruby takes to execute
a script.
We can see an example of this with a simple time-reporting script based on Ruby
Reports: http://rubyreports.org/. This script reads a CSV file and generates a simple
PDF based on it:
time_report.rb
#!/usr/local/bin/ruby
require "rubygems"
require "ruport"
How to Read Code | 53
table = Table('time.csv')
table.to_pdf(:file => 'time.pdf')
We load up this script under ruby-debug:
$ rdebug time_report.rb
./time_report.rb:3 require "rubygems"
(rdb:1)
The debugger breaks at the first line of Ruby code. We will skip over the require
statements with next (abbreviated n; it is the equivalent of ???step over??? in other
debuggers). We can also just hit the Return key to repeat the last command:
./time_report.rb:3 require "rubygems"
(rdb:1) n
./time_report.rb:4 require "ruport"
(rdb:1)
./time_report.rb:6 table = Table('time.csv')
(rdb:1)
Now we are at the first interesting line of code. We can ???step into??? Ruport??™s code
(follow the function call downward) with step or s:
./time_report.rb:6 table = Table('time.csv')
(rdb:1) s
/usr/local/lib/ruby/gems/1.8/gems/ruport-1.2.0/lib/ruport/data/table.rb:805 table=
(rdb:1)
Now we are in Ruport??™s library code, but we don??™t have enough context to know
what is going on.
Pages:
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91