What is Ruby debugger?

Why do we need a debugger?

A debugger is a tool that allows us to dissect and examine our code at different stages. Typically, when we compile a program and there’s an error, it does not give any meaningful explanation about the error. In such cases, it can be hard to locate and resolve the problem.

There are two common types of errors:

  1. Syntax error
  2. Logical error

A syntax error does not compile the code until we fix the syntax. With a logical error, the code compiles without any issues, but when we execute the program, it gives unexpected results.

A debugger helps us locate and fix ​logical errors efficiently that, in some cases, would be a nightmare to fix without a debugger.

Using a Ruby Debugger

Ruby has a debug library that lets us examine our code at every step. To use the debugger, we can run:

ruby -r debugger {.rb FileName}

Do not forget to add -r as it will stop the debugger from executing the code at line number one and asks the user what to do.

Options

After we start the debugger, we will be given a lot of options:

Command Description
b {x} Set a breakpoint at line number x
wat {expression} Set watchpoint
b Displays breakpoints and watchpoints
del {x} Delete breakpoint at line x
disp {expression} Displays value of expression
undisp {x} Undisplays the display expression at number x
c Continue execution
w Displays stackframe
up {x} Moves up x levels in stack frame
down {x} Moves down x levels in stack frame
fin Finish execution of current method
q Quit debugger
v {g} Displays global variables
v {l} Displays local variables
v {i} {object} Displays instance variables of object
v {c} {object} Displays constants of object
m {i} {object} Displays instance methods of object
m {class/module} Displays instance methods of a class/module
n Executes next line
th {l} Displays thread list
th {c} Displays current thread
th {x} Stops thread at x
h Help

Code

Here is a pretty straightforward code that takes two names and then displays the full name. Try executing it:

class FullName
def initialize( fname, lname )
@fname = fname
@lname = lname
@fullname = fname + " " + lname
end
def displayname
puts "My name is #{@lname}, #{@fullname}"
end
end
my_name = FullName.new( "Bond", "James" )
my_name.displayname

Something does not feel right – the first name and the last name seem to be flipped.

Let’s try debugging the code:

Debugging the code
Debugging the code

The above terminal shows a sequence of commands:

  1. Run the debugger with the -r flag.
  2. Set breakpoints at line 13 and line 14.
  3. Go to line 13 using the next line command (n) and check the local variables. It is nil because the line has not yet been executed.
  4. Go to line 14 and check the local variables again. Upon closer look, it seems we’ve initialized the object instance with the wrong first name and last name.

We have debugged the problem now. Flipping the arguments passed to our objects gives us the correct, full name of our 007 agent.

class FullName
def initialize( fname, lname )
@fname = fname
@lname = lname
@fullname = fname + " " + lname
end
def displayname
puts "My name is #{@lname}, #{@fullname}"
end
end
my_name = FullName.new( "James", "Bond" )
my_name.displayname

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved