开发者

How do I set a variable in the Ruby debugger (ruby-debug/rdebug)?

Let's suppose I have a very simple Ruby program:

require 'rubygems'
require 'ruby-debug'
x = 1
debugger
puts x

When execution hits the 'debugger' command it properly halts and gives me the 'rdb' prompt.

Now: my goal is to have the program print 2 instead of 1. How to accomplish this? If I type 'help' then rdebug tells me:

ruby-debug help v0.10.4
Type 'help <command-name>' for help on a specific command

Available commands:
backtrace  delete   enable  help  meth开发者_运维知识库od  putl     set     trace    
break      disable  eval    info  next    quit     show    undisplay
catch      display  exit    irb   p       reload   step    up       
condition  down     finish  kill  pp      restart  thread  var      
continue   edit     frame   list  ps      save     tmate   where

The 'set' command looks promising ... but:

(rdb:1) help set
Modifies parts of the ruby-debug environment. Boolean values take
on, off, 1 or 0.
You can see these environment settings with the "show" command.

-- 
List of set subcommands:
--  
set annotate -- Set annotation level
set args -- Set argument list to give program being debugged when it is started
set autoeval -- Evaluate every unrecognized command
set autolist -- Execute 'list' command on every breakpoint
set autoirb -- Invoke IRB on every stop
set autoreload -- Reload source code when changed
set basename -- Report file basename only showing file names
set callstyle -- Set how you want call parameters displayed
set debuggertesting -- Used when testing the debugger
set forcestep -- Make sure 'next/step' commands always move to a new line
set fullpath -- Display full file names in frames
set history -- Generic command for setting command history parameters
set keep-frame-bindings -- Save frame binding on each call
set linetrace+ -- Set line execution tracing to show different lines
set linetrace -- Set line execution tracing
set listsize -- Set number of source lines to list by default
set trace -- Display stack trace when 'eval' raises exception
set width -- Number of characters the debugger thinks are in a line    

No dice. What to do?

I looked at the Official ruby-debug doc and I looked at the RailsGuides Debugging Rails Applications doc but didn't see the answer.


Both Mike and noodi's answers are great! Note though that "finish" in Mike's example goes to the end of the method or block (which here is in fact the end of the program) while "continue" in noodi's solution continues execution.

Also note that there if "set autoeval" is set "on" then, anything you type at the (rdb) prompt that is not a command is automatically evaluated. Therefore you would not need to type "p x=1", but instead could enter "x=1" and not have to go into irb to be able to avoid "p" or "eval".

In the newer trepanning series debuggers, https://github.com/rocky/rb-trepanning/wiki and https://github.com/rocky/rbx-trepanning/wiki, this is set on by default.


You need to use eval or p and then finish to continue through the script. Something like:

(rdb:1) p x=2
2
(rdb:1) finish
2


→ ruby temp.rb 
temp.rb:5
puts x
(rdb:1) irb
ruby-1.9.2-p0 > x
 => 1 
ruby-1.9.2-p0 > x = 2
 => 2 
ruby-1.9.2-p0 > ^D
temp.rb:5
puts x
(rdb:1) cont
2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜