Updated April 6, 2023
Introduction to Ruby Debugger
The debugger in Ruby is a very useful tool to handle and track the bugs, many times it is very hard for a developer to find exact bugs when they are working on the production issue as in that case developers do not have too much access to manipulate the code, so with the help of debugging’s the various command in Ruby we can track any live issue. In debug, we can easily reach to any particular line or we can easily make break-out point, we can put any checkpoint at any stage of running codes, we can use debug -r filename, and the whole file will be debugged without making any bigger changes into the file.
Syntax
This a very simple syntax for the debug in Ruby, we can use some attributes also for the file at the initial execution.
ruby -r debug filename [...]
Examples of Ruby Debugger Commands
We have created a file test.rb and this file contain the code below. The code below is a file code that contains a Human class and few methods to display the output for the human type details. With the help of the debug various commands, we will debug this file.
class Human
# Method for initialisation inside the class
def initialize(name ,age ,sex )
# Initialising the variables
@name = name
@age = age
@sex =sex
end
def display_data()
puts "The name of the user is #{@name} and age is #{@age} also he is a #{@sex}"
end
end
# Creating an objects and passing parameters for initialization
object1 = Human.new("ranjan", 31, "male")
object2 = Human.new("anjali", 28, "female")
object1.display_data()
object2.display_data()
Example #1 – (debug)
Below is a very simple example where we are running the debug command on the file test.rb.The file test.rb we have created above.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Output:
Example #2 – (b)
Below is an example of putting a breakpoint in the program by using command b of debug, here breakpoint is the point where we wanted to stop the execution of the program. So if we pass the line number then execution will be stopped at line number 11.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Debug.rb
Emacs support available.
test.rb:1:class Human
(rdb:1) b 11
Set breakpoint 1 at test.rb:11
Output:
Example #3 – (c)
In this example we have used the command c of debug, this command instructs the debug that continues the execution.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Debug.rb
Emacs support available.
test.rb:1:class Human
(rdb:1) b 11
Set breakpoint 1 at test.rb:11
(rdb:1) c
The name of the user is ranjan and age is 31 also he is a male
The name of the user is anjali and age is 28 also he is a female
Output:
Example #4 – (s)
In case if we wanted to execute the next line of the code we can use the command s of the debugger.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Debug.rb
Emacs support available.
test.rb:1:class Human
(rdb:1) s 11
test.rb:16:object1.display_data()
(rdb:1) s 2
The name of the user is ranjan and age is 31 also he is a male
test.rb:17:object2.display_data()
(rdb:1) s 2
The name of the user is anjali and age is 28 also he is a female
Output:
Example #5
To move up by level n we can use the command up.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Debug.rb
Emacs support available.
test.rb:1:class Human
(rdb:1) up 3
At toplevel
#1 test.rb:1
(rdb:1) up 5
At toplevel
#1 test.rb:1
(rdb:1)
Output:
Example #6 – (down)
To move the down by level n we can use the command down of the debug. Here n is the number of levels we wanted to put down.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Debug.rb
Emacs support available.
test.rb:1:class Human
(rdb:1) down 5
At stack bottom
#1 test.rb:1
(rdb:1) down 7
At stack bottom
#1 test.rb:1
(rdb:1) down 11
At stack bottom
#1 test.rb:1
(rdb:1) down 15
At stack bottom
#1 test.rb:1
(rdb:1)
Output:
Example #7 – (q)
In the case at any point of time want to stop debugging or simply we want to exit we can use the command q.bOnce we run the command q it will ask for confirmation.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Debug.rb
Emacs support available.
test.rb:1:class Human
(rdb:1) q
Really quit? (y/n) y
Output:
Example #8 – (l)
Suppose at the time debugging we want to see the line of codes for one specific point to another specific point then we can use the command l with start and endpoint. In the below example we have taken l[1 – 20], the output it printed the whole code from like 1 to 20.
Please see the below example along with the screen of the output
Code:
ruby -r debug test.rb
Debug.rb
Emacs support available.
test.rb:1:class Human
(rdb:1) l [1-20]
[0, 20] in test.rb
=> 1 class Human
2 # Method for initialisation inside the class
3 def initialize(name ,age ,sex )
4 # Initialising the variables
5 @name = name
6 @age = age
7 @sex =sex
8 end
9 def display_data()
10 puts "The name of the user is #{@name} and age is #{@age} also he is a #{@sex}"
11 end
12 end
13 # Creating an objects and passing parameters for initialization
14 object1 = Human.new("ranjan", 31, "male")
15 object2 = Human.new("anjali", 28, "female")
16 object1.display_data()
17 object2.display_data()
18
Conclusion
From this tutorial we learned the basic concept of the debugger in Ruby, we learned about the syntax of the debug. We also learned various commands debugged in Ruby with examples. We learned some of the important uses of the debug commands in the real-life world.
Recommended Articles
We hope that this EDUCBA information on “Ruby Debugger” was beneficial to you. You can view EDUCBA’s recommended articles for more information.