I run Ubuntu 10.10. I just want to debug a simple script. After spending half a day trying to figure out how that could be done I give up. What the heck am I supposed to do?
The best debugger I've ever used for Ruby is the one built in to Netbeans. You have to install the fast ruby debugger gem from Netbeans (I'm not sure which gem it actually is, but Netbeans prompts you to do it). I find that it works much better if you switch Netbeans away from the built-in JRuby 1.4 to your system's default Ruby installation. There's also the breakpoint
gem that's worth taking a look at, and using the Ruby built-in library logger
from the start of your development is also helpful. Good luck!
Try default Ruby Debugger either by:
ruby -r debug filename[, ...]
Or if it's CLI script, just change its first line from:
#!/usr/bin/env ruby
to:
#!/usr/bin/env ruby -rdebug
and the script will stop on each Exception.
Or check the following script sample:
#!/usr/bin/env ruby
class Hello
def initialize( hello )
@hello = hello
end
def hello
@hello
end
end
salute = Hello.new( "Hello, Mac!" )
puts salute.hello
You can debug it as shown below:
# ruby -r debug hello.rb
Debug.rb
Emacs support available.
hello.rb:3:class Hello
(rdb:1) v l
salute => nil
(rdb:1) b 10
Set breakpoint 1 at hello.rb:10
(rdb:1) c
Hello, Mac!
Source: Ruby Debugger
Alternatively use lldb
/gdb
. See below the simple example to print script backtrace into foreground:
echo 'call (void)rb_backtrace()' | lldb -p $(pgrep -nf ruby)
Replace lldb
with gdb
if works better. Prefix with sudo
to debug non-owned process.
In Ruby:
ruby -rdebug myscript.rb then,
In Rails: Launch the server with
script/server --debugger
and add debugger in the code.