Debugging in Rails 7

·

1 min read

In Rails 5, byebug is a standard debugger to trace program execution flow and inspect the call stack within an application. From Rails 7, byebug has been replaced with ruby/debug and it comes with colors to highlight syntax. ruby/debug is included in Ruby 3.1 onwards.

Hit and evaluate

Let's say someone enters an URL localhost:3000/subjects/1 in the browser, it will then call show method in subject controller with parameter id 1. The browser shows non-stop spinning circle and once we open up our terminal, it reaches to a debugger breakpoint

 # app/controllers/subject_controller.rb   
     8|   def show
     9|     @subject = Subject.find(params[:id])
=>  10|     binding.break
    11|     render json: @subject
    12|   end
    13| end

and if we trace the subject instance variable, it will return us the subject with id 1.

(ruby) @subject
#<Subject:0x000000010a852e00
 id: 1,
 name: "Data Structures and Algorithms",
 code: "COMP9xxx",
 created_at: Mon, 05 Dec 2022 08:36:16.828535000 UTC +00:00,
 updated_at: Mon, 05 Dec 2022 08:38:13.160839000 UTC +00:00>
(rdbg) c    # continue command
Completed 200 OK in 10259ms (Views: 0.5ms | ActiveRecord: 0.4ms | Allocations: 106092)

In a high-level overview, both byebug and ruby/debug are working similarly.