1. Computing

Setting Displays

By

You can set displays on variables. A "display" simply means you want the debugger to print its value every time you single-step. This is very useful for watching variables change as the code executes. The command for setting a display is the disp command, whose argument is the expression to execute and display each time you single-step. Note that this is an expression. It's Ruby code, so you can format your data any way you'd like.

$ ruby -rdebug problem.rb data.txt
Debug.rb
Emacs support available.

problem.rb:3:lines = File.readlines(ARGV[0])
(rdb:1) b 7
Set breakpoint 1 at problem.rb:7
(rdb:1) c
Breakpoint 1, toplevel at problem.rb:7
problem.rb:7: nums.map!{|n| n.to_f }
(rdb:1) disp nums.inspect
1: nums.inspect = ["12.3", "0.3\n"]
(rdb:1) n
problem.rb:7: nums.map!{|n| n.to_f }
1: nums.inspect = ["12.3", "0.3\n"]
(rdb:1) n
problem.rb:7: nums.map!{|n| n.to_f }
1: nums.inspect = [12.3, "0.3\n"]
(rdb:1) n
problem.rb:9: nums[2] = nums[0].to_f / nums[1].to_f
1: nums.inspect = [12.3, 0.3]
(rdb:1) n
problem.rb:11: puts nums.join(',')
1: nums.inspect = [12.3, 0.3, 41.0]
(rdb:1) exit
  1. About.com
  2. Computing
  3. Ruby
  4. Advanced Ruby
  5. Setting Displays

©2014 About.com. All rights reserved.