DEV Community

Hasan Tanbir
Hasan Tanbir

Posted on

Rails 7 introduces a new debugging gem

We commonly use byebug for debugging our code which is an easy-to-use, feature-rich ruby debugger. This gem is introduced in Rails 5. It offers features like Stepping, Breaking, Evaluating and Tracking.

We can easily control the execution of a program and the debug inspector for call stack navigation. It allows us to control and the execution flow.

We can found this documentation for installing byebug with ruby's most popular framework ruby on rails which was marged in this pull request

But Rails 7 is replacing byebug with ruby's own gem debug which was merged in this pull request on Sep 8, 2021.

debug is Ruby's debug mechanism which will be included in Ruby 3.1 version.

So, to adjust Rails with Ruby debug has been introduced in Rails 7.

Let's compare some example of debugging with byebug and new debug gem.

Before

Let's assume we have a controller named UserController. Inside any Rails application, we can call the debugger by calling the byebug method.

# app/controllers/user_controller.rb

class UserController < ApplicationController
  def index
    name = "Hasan Tanbir"
    byebug # Call to debugger
    country = "Bangladesh"
  end
end
Enter fullscreen mode Exit fullscreen mode

Invoked debugger results will be:

    [1, 7] in app/controllers/user_controller.rb
    1: class UserController < ApplicationController
    2:   def index
    3:     name = "Hasan Tanbir"
    4:     byebug # Call to debugger
=>  5:    country = "Bangladesh"
    6:   end
    7: end

  (byebug) name # variable call
  "Hasan Tanbir"
Enter fullscreen mode Exit fullscreen mode

After

When we use debug in Rails 7, we will have to use binding.break method instead of byebug method.

# app/controllers/user_controller.rb

class UserController < ApplicationController
  def index
    name = "Hasan Tanbir"
    binding.break # Call to debugger
    country = "Bangladesh"
  end
end
Enter fullscreen mode Exit fullscreen mode

Invoked debugger results will be:

    [1, 7] in app/controllers/user_controller.rb
    1: class UserController < ApplicationController
    2:   def index
    3:     name = "Hasan Tanbir"
    4:     byebug # Call to debugger
=>  5:    country = "Bangladesh"
    6:   end
    7: end
>#0 UserController#index at ~/demo_app/app/controllers/user_controller.rb:5 

  (rdbg) name # variable call
  "Hasan Tanbir"
Enter fullscreen mode Exit fullscreen mode

You can check out the pull request for more details and for commands or features of Ruby/debug.

Top comments (0)