Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
debugger

What is Ruby debugger?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

svg viewer

Why do we need a debugger?

A debugger is a tool that allows you to dissect and examine your code at different stages. Typically, when you 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 let you compile the code until you fix the syntax. With a logical error, the code compiles without any issues, but when you 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 you examine your code at every step. To use the debugger, you 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 you start the debugger, you 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:

widget

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.

There you go. You have debugged the problem. 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
svg viewer

RELATED TAGS

ruby
debugger
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring