Rekindling Our Relationship with the debugger Byebug
[ It Was Meant To Be]
The Basics — How to Install and Implement
_______________________________________________________
First, a very, very, brief history behind this wonderful gem that is settled in the git repository of (https://github.com/deivid-rodriguez/byebug). It was essentially created in order to give (RORers?) a sharp/quick way of debugging their Rail projects with a debugger tool that has a plethora of functionality and was built off of former rail debugging gems.
In short, Byebug is simply a debugger gem for Ruby code. A debugger (for those of you that don’t know by now -(we have been mostly using pry)… allows you to pause execution at any given line of code, inspect the particular status of the app, and then run code line by line, allowing you to check ( WHETHER / HOW) everything is working
To get the ball rolling, I’ll show how quickly it takes to install the debugging gem and be ready for use within your rails program…
Type the following into your project directory:
or you can add it to the gemfile of your project directory —
However, lucky enough for us “Ruby on Railers”— it’s such a helpful debugger that Rails packs it into it’s gemfile from the start, so that it installs as soon as you create a new rails project!
Implementation of the byebug
_______________________________________
NO MORE (REQUIRE ‘pry’, PRY.start, Binding.pry and praying for a hit) simply just drop a ‘byebug’ in your code!
It is quite easy to implement in your code, as soon as you run your rails server and it hits the index page — it will immediately drop us right into the byebug debugging console!
Cool & Neat functionality within byebug
_____________________________________________________
As one of my best pals always said….
“Let’s kick it up a notch!” — Emeril Lagasse
_______________________________________________________
…before we dive head-first into the commands because byebug has been working on itself while we were all lost in the eyes of pry, and now we have to add one more gem to our gemfile to get the full functionality we desire
This allows us to use these additional/key commands:
Line by line: Next, Step, Finish, Continue
Stack Navigation: Backtrace, Up, Down, Frame
1.(n)ext: ( Runs next [one or more] lines of code)
2. (f)rame: (shows information about the current working stack frame, including both the file and line number the byebug is on)
3. (s)tep: (Runs next/or same a block or method multiple times →or goes to next method if multiple methods inside the file that is holding the byebug)
4. (l)ist/(list=) — (lists shows the previous ten lines of code above your byebug/ adding a “=” will bring you back to your original line of code)
5. down/up: (moves to a higher/lower frame in a stack trace of the file you place your byebug in)
6. (e)dit: (brings you to exact file/line to edit source file using “edit (file_name)”)
7. (v)ar (object args): (shows all the variables within frame scope)
8. (b)reak/(c)ontinue: (set break points — then continue runs the program thru or until the next break point)
9. IRB /Pry: (Can drop into IRB then into pry within byebug (inception-meme))
10. Display/Undisplay: (Allows you to keep track of variables throughout a method to continually see when/what their values change to)
11. save: (saves a byebug session to a file — access them through command “history” in byebug console)
Efficiency within Rails
_________________________
Byebug gem works so smooth in rails that [Ruby on Rails] decided to include it within its gemfile, so since they knock out the first part of my blog by providing it the gemfile — the creators must be providing it for a reason right?…so now you should be all currently asking yourselves: “COOL JOE… BUT WHY? → (HINT: look @ the title)←
Well, if you can’t read or you’ve checked out by now — I’ll let ya in on the secret:
E-F-F-I-C-E-N-C-Y …. Efficiency
_________________________________________________________________
- It’s extremely responsive through the many functionalities (commands) that are built into it, that it makes debugging such a breeze — no more exiting, replacing and re-entering prys!
- It allows us to traverse deeply within our code and even make edits to our actual code without ever having to exit out of the byebug session
- We can also save our byebug sessions to files to later look at certain debugging sessions we were involved with and might need to revisit
- It provides us with great tools to look into any sort of objects and what is associated with them [reg variables, instance variables, controllers, models, etc…]
Why it’s better than MOST
__________________________________
Byebug vs. Binding.pry
______________________________________________________________
“If you cannot beat em… join em” — Kevin ‘The Great Snake’ Durant
_______________________________________________________________
The Only yet…. Biggest Advantage over Pry: Not only do we now have way more functionality in our debugging console, but we also now inherit all of what pry can do — as it’s one of the many commands that comes within byebug — that lets us jump into an IRB, and then into a Pry.
CODE RELATED RESOURCES USED:
- Ruby on Rails Guide for Debugging with Byebug
- Explanation and usage of Byebug
- A great blog on further debugging with Byebug
NON CODE RELATED: TO FOLLOW THE THEME OF THE GIFS :