2

Ruby 프로그램이 매우 간단하다고 가정 해 보겠습니다.어떻게하면 Ruby 디버거 (ruby-debug/rdebug)에서 변수를 설정할 수 있습니까?

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

실행이 '디버거'명령에 도달하면 제대로 중지되고 '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를 보았지만 대답을 보지 못했습니다.

답변

2

스크립트를 계속 진행하려면 eval 또는 pfinish을 사용해야합니다. 뭔가 같은 :

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

와우, 똑똑. p 명령은 표현식을 인쇄하고 'x = 2'는 표현식이므로 변수 x에 2를 대입하면 p 명령의 부작용이 발생합니다. – Purplejacket

+0

Rocky가 말했듯이 "finish"를 사용하거나 메시지 블록의 끝으로 건너 뛰거나 현재 줄에서 실행을 계속할 수 있습니다. – Purplejacket

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 
4

모두 마이크와 noodi의 응답은 중대하다! Mike의 예제에서 "finish"는 메소드 나 블록 (실제로 프로그램의 끝 부분)의 끝으로가는 반면, noodi의 솔루션에서 "continue"는 실행을 계속합니다.

"set autoeval"이 "on"으로 설정된 경우 명령이 아닌 (rdb) 프롬프트에서 입력 한 내용은 자동으로 평가됩니다. 따라서 "p x = 1"을 입력 할 필요가 없으며 대신 "x = 1"을 입력하고 "p"또는 "eval"을 피할 수 있도록 irb에 들어갈 필요가 없습니다.

최신 트레 패닝 시리즈 디버거 인 https://github.com/rocky/rb-trepanning/wikihttps://github.com/rocky/rbx-trepanning/wiki에서는 이것이 기본적으로 설정됩니다.