2

非常に単純な Ruby プログラムがあるとします。

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

実行が「debugger」コマンドに到達すると、コマンドは適切に停止し、「rdb」プロンプトが表示されます。

今:私の目標は、プログラムに1ではなく2を出力させることです。これを達成するにはどうすればよいですか? 「help」と入力すると、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を見ましたが、答えがわかりませんでした。

4

3 に答える 3

4

マイクとノーディの両方の答えは素晴らしいです! ただし、Mike の例の「終了」はメソッドまたはブロックの最後 (ここでは実際にはプログラムの最後) に移動し、noodi のソリューションの「続行」は実行を継続することに注意してください。

また、「set autoeval」が「on」に設定されている場合、(rdb) プロンプトで入力したコマンド以外の入力はすべて自動的に評価されることに注意してください。したがって、「px=1」と入力する必要はありませんが、代わりに「x=1」と入力することができ、「p」または「eval」を回避するために irb に入る必要はありません。

新しい Trepanning シリーズのデバッガーhttps://github.com/rocky/rb-trepanning/wikiおよびhttps://github.com/rocky/rbx-trepanning/wikiでは、これはデフォルトでオンに設定されています。

于 2011-01-13T21:37:15.383 に答える
2

スクリプトを続行するには、evalまたはpを使用する必要があります。finish何かのようなもの:

(rdb:1) p x=2
2
(rdb:1) finish
2
于 2011-01-13T20:30:03.153 に答える
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
于 2011-01-13T20:30:57.697 に答える