【问题标题】:How do I set a variable in the Ruby debugger (ruby-debug/rdebug)?如何在 Ruby 调试器 (ruby-debug/rdebug) 中设置变量?
【发布时间】:2011-01-13 20:16:47
【问题描述】:

假设我有一个非常简单的 Ruby 程序:

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

当执行到“调试器”命令时,它会正确停止并给我“rdb”提示。

现在:我的目标是让程序打印 2 而不是 1。如何做到这一点?如果我输入“帮助”,那么 rdebug 会告诉我:

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

Available commands:
backtrace  delete   enable  help  method  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

“set”命令看起来很有希望……但是:

(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    

没有骰子。怎么办?

我查看了Official ruby-debug doc,也查看了RailsGuides Debugging Rails Applications doc,但没有看到答案。

【问题讨论】:

    标签: ruby-on-rails ruby debugging variable-assignment ruby-debug


    【解决方案1】:

    Mike 和 noodi 的回答都很棒!请注意,尽管 Mike 的示例中的“完成”是到方法或块的末尾(这里实际上是程序的结尾),而 noodi 的解决方案中的“继续”继续执行。

    另请注意,如果“set autoeval”设置为“on”,那么您在 (rdb) 提示符下键入的任何不是命令的内容都会被自动评估。因此,您不需要键入“p x=1”,而是可以输入“x=1”,而不必进入 irb 即可避免“p”或“eval”。

    在较新的钻孔系列调试器https://github.com/rocky/rb-trepanning/wikihttps://github.com/rocky/rbx-trepanning/wiki 中,默认设置为启用。

    【讨论】:

      【解决方案2】:

      您需要使用evalp 然后finish 继续执行脚本。比如:

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

      【讨论】:

      • 哇,聪明。 p 命令打印表达式,而 'x=2' 是一个表达式,因此将 2 分配给变量 x 成为 p 命令的副作用。
      • 正如 Rocky 所说,我可以像你一样使用“完成”,跳到消息块的末尾,或者“继续”从当前行继续执行。
      【解决方案3】:
      → 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
      

      【讨论】:

        猜你喜欢
        • 2011-02-28
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        • 2014-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-24
        相关资源
        最近更新 更多