GAURAV VARMA
Rails 5 introduced byebug which is
an easy-to-use, feature-rich ruby debugger. It offers features like Stepping
,
Breaking
, Evaluating
, Tracking
.
Using byebug we can easily control the execution of a program and the debug inspector for call stack navigation. This allows us to handle and track the execution flow.
Here is byebug documentation and here is the pull request where it was added.
Rails 7 is replacing byebug with ruby/debug.
debug
is Ruby’s new debugger which will be included in Ruby 3.1. To align
Rails with Ruby debug
has been added to Rails 7.
Let's see an example of debugging with both Byebug and Debug.
Before
Let's assume we have a NameController
. Inside any Rails application, you can
call the debugger by calling the byebug
method.
1# app/controllers/name_controller.rb
2class NameController < ApplicationController
3 def index
4 name = "John Doe"
5 byebug # Call to debugger
6 city = "San Francisco"
7 end
8end
Then The invoked debugger results in the following.
1 [1, 7] in app/controllers/test_controller.rb
2 1: class NameController < ApplicationController
3 2: def index
4 3: name = "John Doe"
5 4: byebug # Call to debugger
6=> 5: city = "San Francisco"
7 6: end
8 7: end
9
10 (byebug) name # variable call
11 "John Doe"
Rails 7 onwards
We can use the binding.break
method for calling Ruby Debug.
1# app/controllers/name_controller.rb
2class NameController < ApplicationController
3 def index
4 name = "John Doe"
5 binding.break # Call to debugger
6 city = "San Francisco"
7 end
8end
The invoked debugger results in the following.
1 [1, 7] in app/controllers/test_controller.rb
2 1| class NameController < ApplicationController
3 2| def index
4 3| name = "John Doe"
5 4| binding.break # Call to debugger
6> 5| city = "San Francisco"
7 6| end
8 7| end
9>#0 NameController#index at ~/demo_app/app/controllers/name_controller.rb:5
10 #1 ActionController::BasicImplicitRender#send_action(method="index", args=[])
11(rdbg) name # variable call
12"John Doe"
Check out this pull request for more details and for commands or features of Ruby Debug, please visit Ruby-Debug.
This article was originally published on this website.