# Q: How to initialize debugger's state when starting

**URL:** <https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456>\
**Category:** Ruby\
**Created:** [February 23, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456 "2006-02-23T18:40:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dumaiu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@Dumaiu](https://www.ruby-forum.com/u/Dumaiu)\
**Post date:** [February 23, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456/1 "2006-02-23T18:40:16Z")

</div>

Quick question:

When starting a Ruby script in debug (’-rdebug’) mode, what’s the  
simplest way to source an \*rc-type file for, say, setting breakpoints?  
Is it possible to do such during the invocation of `ruby` at the  
command-line?

Thanks,  
–J J-S

---

<div class="post-metadata">

**Author:** ![Dumaiu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@Dumaiu](https://www.ruby-forum.com/u/Dumaiu)\
**Post date:** [February 23, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456/2 "2006-02-23T20:50:45Z")

</div>

PS:  
(What I’m looking for is something with the effect of gdb’s ‘-x’  
option.)

---

<div class="post-metadata">

**Author:** ![Dumaiu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@Dumaiu](https://www.ruby-forum.com/u/Dumaiu)\
**Post date:** [February 23, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456/3 "2006-02-23T23:53:29Z")

</div>

On Feb 23, 2006, at 2:48 PM, Dumaiu wrote:

> PS:  
> (What I’m looking for is something with the effect of gdb’s ‘-x’  
> option.)

Well after looking at debug.rb and trying to hack this in, I think  
your best bet is to look at one of the GUI debuggers (like Mr. Guid  
or Emacs) that talk to rdb and write a little frontend to it that  
would have this capability. Unfortunately, its setup like

def do\_command  
input = readline  
…  
end

instead of

def do\_command(input)  
…  
end

do\_command(readline)

so I gave up trying to hack in this functionality directly. Good  
luck, though

---

<div class="post-metadata">

**Author:** ![Dumaiu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@Dumaiu](https://www.ruby-forum.com/u/Dumaiu)\
**Post date:** [February 24, 2006, 12:54am UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456/4 "2006-02-24T00:54:20Z")

</div>

Thanks. I _thought_ it would be a quick question… one of those  
It’s-so-obvious-I-can’t-see-it moments. Because I’m not much for  
IDE’s, I think I’ll wait to see if anyone else phones in and then maybe  
take a bite out of debug.rb myself.

-J

---

<div class="post-metadata">

**Author:** ![Dumaiu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@Dumaiu](https://www.ruby-forum.com/u/Dumaiu)\
**Post date:** [February 24, 2006, 1:54am UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456/5 "2006-02-24T01:54:49Z")

</div>

A short hack.  
Make a copy of debug.rb and put these lines right after the Context  
class.

class Context  
alias original\_readline readline  
def readline(prompt, hist)  
@rc\_file ||= File.readlines(“debug.rc”)  
if @rc\_file.empty?  
original\_readline(prompt, hist)  
else  
@rc\_file.shift  
end  
end  
end

Now create a file debug.rc with your desired breakpoint:  
b 100  
b 200

Start the debugger:  
ruby -r./debug myscript.rb

---

<div class="post-metadata">

**Author:** ![Dumaiu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@Dumaiu](https://www.ruby-forum.com/u/Dumaiu)\
**Post date:** [February 24, 2006, 2:43am UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456/6 "2006-02-24T02:43:10Z")

</div>

On Feb 23, 2006, at 7:53 PM, Matthias G. wrote:

> ```
> else
> 
> ```
> 
> ruby -r./debug myscript.rb

Clever. I hope if someone comes up with a clean way to do this it  
gets merged in. It seems like a good idea.

---

<div class="post-metadata">

**Author:** ![Dumaiu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@Dumaiu](https://www.ruby-forum.com/u/Dumaiu)\
**Post date:** [February 28, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/q-how-to-initialize-debuggers-state-when-starting/72456/7 "2006-02-28T15:24:31Z")

</div>

On 2/23/06, Matthias G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> else
> 
> ```
> 
> ruby -r./debug myscript.rb  
> Cool idea! However, if you don’t have a debug.rc file, this doesn’t  
> work. I tried to change it to handle that case as follows, but it’s  
> still not quite right. Breakpoints I set in debug.rc are listed when I  
> run “b”, but when I run “c” to continue execution, it doesn’t stop at  
> them. However, if I issue the same break commands in the debugger,  
> they work. Can you see what I’m doing wrong?

class Context  
alias original\_readline readline  
def readline(prompt, history)  
# If config file exists and hasn’t been read yet, read it.  
if not @rc\_file and File.exists?(‘debug.rc’)  
@rc\_file = File.readlines(‘debug.rc’)  
end

```
if @rc_file and not @rc_file.empty?
  @rc_file.shift
else
  original_readline(prompt, history)
end

```

end  
end
