1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Stepping

By

The act of "stepping through" a program is to have the debugger stop program execution after every line of source code. You can then verify variables are as you expect them to be. Stepping through your code often is recommended for many languages and it certainly can't hurt in Ruby. In the following example, after setting a breakpoint in the each loop, we step through the code using the n command. When you've finished single-stepping, use the c command to tell the debugger to continue running until the next breakpoint is reached.

(rdb:1) l
[-2, 7] in problem.rb
1
2
=> 3 lines = File.readlines(ARGV[0])
4
5 lines.each do|l|
6 nums = l.split /,/
7 nums.map!{|n| n.to_f }
(rdb:1) b 5
Set breakpoint 1 at problem.rb:5
(rdb:1) c
Breakpoint 1, toplevel at problem.rb:5
problem.rb:5:lines.each do|l|
(rdb:1) n
problem.rb:6: nums = l.split /,/
(rdb:1) n
problem.rb:7: nums.map!{|n| n.to_f }
(rdb:1) n
problem.rb:7: nums.map!{|n| n.to_f }
(rdb:1) n
problem.rb:7: nums.map!{|n| n.to_f }
(rdb:1) n
problem.rb:9: nums[2] = nums[0].to_f / nums[1].to_f
(rdb:1) puts nums
12.3
0.3
nil

Note that the puts "command" is used. The puts line is not a special command. Since the Ruby debug prompt is actually an interactive Ruby prompt you can use any Ruby code you wish during your Ruby debug sessions.

  1. About.com
  2. Technology
  3. Ruby
  4. Advanced Ruby
  5. Stepping

©2014 About.com. All rights reserved.