paint-brush
Professional Debugging in Railsby@fatymahmed
428 reads
428 reads

Professional Debugging in Rails

by fatima ahmedDecember 27th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Debugging is as important as writing code, it can cause your program to crash or behave unexpectedly. Byebug is a working breakpoint, this means you can inspect your data at any stage where you place the breakpoint. pry is very similar with byebug but it isn’t automatically shipped with rails, so you have to install it yourself. To use pry on your application, simple place the code below on where you’d like to inspect. It’s similar to the debugger in the console for JavaScript.

Company Mentioned

Mention Thumbnail
featured image - Professional Debugging in Rails
fatima ahmed HackerNoon profile picture

Debugging is as important as writing code, it can cause your program to crash or behave unexpectedly. Most of the time, debugging can be very stressful, especially when one doesn’t use the right tools to debug.

When I started building apps in rails, I used the helper method inspect to inspect my code, but it got tiresome to type inspect on each data that I wanted to inspect, so I decided to spend some time learning debugging tools that made it easier and faster to debug.

In this article, I shall talk about a few tools that help me so much when I debug my code.

Byebug

Byebug is a gem which is automatically shipped with rails, you can find it in gemfile in your app directory. Byebug is a working breakpoint, this means you can inspect your data at any stage where you place the breakpoint, all you have to do is to type byebug where you’d like to inspect and run the app.

Once the app reaches the breakpoint, the server hangs and a terminal appears and you can type anything that you’d like to inspect.

Another great feature of byebug is that you continue or step into any function calls and inspect them too. It’s similar to the debugger in the console for JavaScript. 

To continue running the app until the next byebug command or the end of your application if you don’t have any, you simply type continue in the terminal, step to step into and next to move to the next line of code.

byebug debugger placed on index action

Use byebug to inspect books

use next command to execute next line of code

PRY

Pry is very similar with byebug but it isn’t automatically shipped with rails, So you have to install it yourself.

To install pry, simply add 

gem ‘pry-byebug’

in your gemfile then run

bundle install

or check out the pry’s documentation for more information.

To use pry on your application, simple place the code below on where you’d like to inspect

<% binding.pry %>

using pry as a debugger

Nicer syntax with pry

An advantage of using pry over byebug is that pry returns a nicer format of the data you requested for, it also highlights the syntax of the code around the breakpoint.

You can can place more than one "byebug" or "pry" in a single file and use ‘next’ to step into the next line of the code or 'continue' if you want your code to run till the end if there's only one "byebug" or pry in the file.

To quit the pry terminal you should type ":q", to navigate up and down, you could use "j" and "k" respectively.

Resources