ฉันจะตั้งค่าตัวแปรใน Ruby debugger (ruby-debug/rdebug) ได้อย่างไร

สมมติว่าฉันมีโปรแกรม Ruby ที่ง่ายมาก:

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

เมื่อการดำเนินการกระทบคำสั่ง 'ดีบักเกอร์' มันจะหยุดลงอย่างถูกต้องและให้พรอมต์ 'rdb' แก่ฉัน

ตอนนี้: เป้าหมายของฉันคือให้โปรแกรมพิมพ์ 2 แทนที่จะเป็น 1 จะทำสิ่งนี้ให้สำเร็จได้อย่างไร? ถ้าฉันพิมพ์ '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    

ไม่มีลูกเต๋า จะทำอย่างไร?

ฉันดูที่ เอกสาร ruby-debug อย่างเป็นทางการ และฉันดูที่ RailsGuides Debugging Rails Applications doc แต่ไม่เห็นคำตอบ


person Purplejacket    schedule 13.01.2011    source แหล่งที่มา


คำตอบ (2)


คุณต้องใช้ eval หรือ p จากนั้น finish เพื่ออ่านสคริปต์ต่อ สิ่งที่ต้องการ:

(rdb:1) p x=2
2
(rdb:1) finish
2
person Mike Gorski    schedule 13.01.2011
comment
ว้าวฉลาด คำสั่ง p พิมพ์นิพจน์และ 'x=2' คือนิพจน์ ดังนั้นการกำหนด 2 ให้กับตัวแปร x จึงกลายเป็นผลข้างเคียงของคำสั่ง p - person Purplejacket; 14.01.2011
comment
ดังที่ Rocky บอกว่าฉันสามารถใช้ Finish ได้ตามที่คุณทำ กระโดดไปที่จุดสิ้นสุดของบล็อกข้อความ หรือดำเนินการต่อจากบรรทัดปัจจุบัน - person Purplejacket; 19.01.2011

คำตอบของทั้ง Mike และ noodi นั้นยอดเยี่ยมมาก! โปรดทราบว่า "เสร็จสิ้น" ในตัวอย่างของ Mike ไปที่จุดสิ้นสุดของวิธีการหรือบล็อก (ซึ่งอันที่จริงนี่คือจุดสิ้นสุดของโปรแกรม) ในขณะที่ "ดำเนินการต่อ" ในโซลูชันของ noodi ยังคงดำเนินการต่อไป

โปรดทราบว่าหากมีการตั้งค่า "set autoeval" เป็น "on" ดังนั้นสิ่งที่คุณพิมพ์ที่พรอมต์ (rdb) ที่ไม่ใช่คำสั่งจะถูกประเมินโดยอัตโนมัติ ดังนั้นคุณไม่จำเป็นต้องพิมพ์ "p x=1" แต่สามารถป้อน "x=1" แทนได้ และไม่ต้องป้อน irb เพื่อหลีกเลี่ยง "p" หรือ "eval"

ในดีบักเกอร์ซีรีส์ Trepanning รุ่นใหม่ https://github.com/rocky/rb-trepanning/wiki และ https://github.com/rocky/rbx-trepanning/wiki ซึ่งจะถูกตั้งค่าไว้เป็นค่าเริ่มต้น

person rocky    schedule 13.01.2011