Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
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 therequire
statements withnext(abbreviatedn; 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) withstep ors:


./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. Using thelist (l) command, we show the context from the source:


(rdb:1) l
[800, 809] in .../gems/ruport-1.2.0/lib/ruport/data/table.rb
800 # # accepts all Data::Table.load options, including block
801 #
802 # t = Table("foo.csv")
803 # t = Table("bar.csv", :has_names => false)
804 def Table(*args,&block)
=> 805 table=
806 case(args[0])
807 when Array
808 opts = args[1] || {}
809 Ruport::Data::Table.new(f={:column_names => args[0]}.
merge(opts),&block)

Thebacktrace(bt) command shows the series of stack frames in which we are
nested:


(rdb:1) bt
--> #0 .../gems/ruport-1.2.0/lib/ruport/data/table.rb:805
in 'Table'
#1 ./time_report.rb:6
(rdb:1)

And we can step through the code until we have satisfied our curiosity, at which point
we usecont (c) to exit the debugger and allow the program’s execution to continue:

Free download pdf