Bye, Bye Bug!

Kristina Tong
Sep 5, 2018 · 4 min read

Most of us have used binding.pry or byebug as a way to test our code, or identify errors in our applications. As we dive deeper into building our Rails applications, our code base will only get larger and more complex. If our application is not working, debugging is a great way to narrow down exactly where something may be breaking and why.

In addition to fixing bugs, many of the methods we use are inherited from other classes of code. Our models inherit from ActiveRecord::Base, our migrations inherit from ActiveRecord::Migration, and our controllers inherit from ActionController::Base. How will we ever be able to see these pieces of code that we did not write ourselves? Debugging! A great way to find out exactly how the computer processes each line of code is through debugging! Byebug is a powerful debugging tool offered in Rails that gives us many options to step through code, use breakpoints, and much more.

Getting Set Up

Version 5.0 of Rails comes with Byebug, but if you don’t have it, you can simply add the following to your Gemfile and run bundle:

group :development, :test do
gem 'byebug'
end

Simply drop byebug wherever you want to start debugging and the execution will stop there.

def index
@artists = Artist.all
byebug
end

Once the execution gets to your byebug command you will get a debugging prompt. Byebug will automatically list 10 lines of code centered around the current line every time it is stopped. The current line is marked with =>.

[12, 21] in /Users/kristina/flatiron/rails-playlister/app/controllers/artists_controller.rb
12: end
13:
14: def index
15: @artists = Artist.all
16: byebug
=> 17: end
18:
19: def new
20: @artist = Artist.new
21: end

To inspect a variable, type the variable name in the byebug session:

(byebug) @artists
Artist Load (0.3ms) SELECT "artists".* FROM "artists"
#<ActiveRecord::Relation [#<Artist id: 1, name: "A Tribe Called Quest", bio: "A Tribe Called Quest was an American hip-hop colle...", created_at: "2018-09-04 20:01:36", updated_at: "2018-09-05 05:20:13">, #<Artist id: 2, name: "Kanye West", bio: "Kanye Omari West (born June 8, 1977) is an America...", created_at: "2018-09-05 05:02:20", updated_at: "2018-09-05 05:19:19">]>

Useful Commands

  • q[uit]! or exit! — Get out of the debugger and exit your program
  • c[ontinue] <line-number> — Continue running the program until it ends, hits a breakpoint or reaches line line-number (if specified).
  • n[ext] <number> — Go to the next line, stepping over method calls. If number specified, go forward that number of lines.
  • s[tep] <number> — Go to the next line, stepping into method calls line by line. If number is specified, make that many steps.
[1, 10] in /Users/kristinatong/.rvm/gems/ruby-2.3.3/gems/actionpack-4.2.10/lib/action_controller/metal/implicit_render.rb
1: module ActionController
2: module ImplicitRender
3: def send_action(method, *args)
4: ret = super
=> 5: default_render unless performed?
6: ret
7: end
8:
9: def default_render(*args)
10: render(*args)

Breakpoints

  • b[reak] <number> — Set a breakpoint in line number specified in the current source file.
[29, 38] in /Users/kristina/flatiron/rails-playlister/app/controllers/artists_controller.rb
29: redirect_to artist_path(@artist)
30: end
31:
32: def create
33: byebug
=> 34: @artist = Artist.new
35: @artist.name = params[:artist][:name]
36: @artist.bio = params[:artist][:bio]
37: @artist.save
38: redirect_to artist_path(@artist)
(byebug) break 38
Created breakpoint 1 at /Users/kristina/flatiron/rails-playlister/app/controllers/artists_controller.rb:38
(byebug) continue
Stopped by breakpoint 1 at /Users/kristina/flatiron/rails-playlister/app/controllers/artists_controller.rb:38
[31, 40] in /Users/kristina/flatiron/rails-playlister/app/controllers/artists_controller.rb
31:
32: def create
33: byebug
34: @artist = Artist.new
35: @artist.name = params[:artist][:name]
36: @artist.bio = params[:artist][:bio]
37: @artist.save
=> 38: redirect_to artist_path(@artist)
39: end
40: end
  • b[reak] <filename>:<line-number> — Set a breakpoint at line-number in filename (or the current file if filename is blank)
  • b[reak] <class(.|#)<method> — Set a breakpoint at the start of the method method in class class.
  • info breakpoints — List all breakpoints, with status.
(byebug) info breakpoints
Num Enb What
1 y at /Users/kristina/flatiron/rails-playlister/app/controllers/artists_controller.rb:38
breakpoint already hit 1 time
  • del[ete] <number> — Delete a specified breakpoint. If not specified, all breakpoints will be deleted.
(byebug) delete 1
Deleted breakpoint 1

Help

  • h[elp — See all available byebug commands:
(byebug) helpbreak      -- Sets breakpoints in the source code
catch -- Handles exception catchpoints
condition -- Sets conditions on breakpoints
continue -- Runs until program ends, hits a breakpoint or reaches a line
debug -- Spawns a subdebugger
delete -- Deletes breakpoints
disable -- Disables breakpoints or displays
display -- Evaluates expressions every time the debugger stops
down -- Moves to a lower frame in the stack trace
edit -- Edits source files
enable -- Enables breakpoints or displays
finish -- Runs the program until frame returns
frame -- Moves to a frame in the call stack
help -- Helps you using byebug
history -- Shows byebug's history of commands
info -- Shows several informations about the program being debugged
interrupt -- Interrupts the program
irb -- Starts an IRB session
kill -- Sends a signal to the current process
list -- Lists lines of source code
method -- Shows methods of an object, class or module
next -- Runs one or more lines of code
pry -- Starts a Pry session
quit -- Exits byebug
restart -- Restarts the debugged program
save -- Saves current byebug session to a file
set -- Modifies byebug settings
show -- Shows byebug settings
source -- Restores a previously saved byebug session
step -- Steps into blocks or methods one or more times
thread -- Commands to manipulate threads
tracevar -- Enables tracing of a global variable
undisplay -- Stops displaying all or some expressions when program stops
untracevar -- Stops tracing a global variable
up -- Moves to a higher frame in the stack trace
var -- Shows variables and its values
where -- Displays the backtrace

More features!

There are tons more you could do with this magical byebug gem! To find out more, visit the byebug GUIDE below:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade