Hello Pry, Buh-bye Bug!

Yura Choi
Yura Choi
Jul 21, 2017 · 4 min read

Happy Friday! I refuse to believe this, but we’re already wrapping up Week 4. Time flies when you’re coding! I think it’s a good sign, that I’m immersed into learning and finding the overall experience at Flatiron School delightful.

We have switched to the second module of the program, which deals with building web frameworks using Rack-based Sinatra and Rails. The truth is, our code is only getting more complex, and the more complex it gets, the more inevitable the errors become.

No matter how deep we’re in, it is always smart to test out our code. When I first learned Ruby, I began the practice of testing out my code in Interactive Ruby. IRB evaluates our input and returns the result. However, IRB provides a blank slate to work with every time, which may be inefficient if we have to keep transferring our code. Then Pry came along. Pry is similar to IRB in its function, but it allows us to work directly inside our program.

Pry

require 'pry'
class Dog
def bark
puts "Woof!"
end
def sit
puts "The Dog is sitting"
end
end
binding.pry

We first need to include gem ‘pry’ in our Gemfile. As shown above, in order to use the gem Pry, we must require ‘pry’ at the top of our file and place binding.pry within the block we desire to test. In this case, the context of pry is main.

Our code gets evaluated and the result gets printed out on to the terminal.

You will notice that pry is more convenient than IRB since we didn’t have to copy and paste our class and methods onto the shell.

Then how do errors in pry look like?

NoMethodError in Pry

Pry displayed a NoMethodError since we haven’t defined a “howl” method. So what can we do to solve this particular error?

  1. Create a howl method
  2. Don’t call on howl method (it doesn’t exist until you create it)

Pry is cool and useful because we get to FEEL and BREATHE the errors instead of avoiding them. But then… I noticed our instructors using a special, by invite-only debugger called ‘byebug’ whenever pry got them frustrated.

NoMethodError detected using ‘byebug’

Byebug

Frankly, I was intimidated by trying out byebug. I didn’t want anything to do with facing more errors…. I was afraid of generating more errors by looking up more ways to find errors!

Goodbye bug!

I came to a realization that byebug is just another gem like pry that we can use to debug our code. According to “Debugging using ByeBug Gem in Rails 5” written by Bala Paranj, this gem is included in Rails 5 project by default. If you create a new Rails 5 project, your Gemfile should already have gem ‘byebug’ listed. Just don’t forget to ‘bundle install’ your gems.

get '/juices' do
@juices = Juice.all
byebug
erb :'juices/index.html'
end

How to use byebug? We can drop ‘byebug’ anywhere you would like to test as long as you know the gem is in the Gemfile. (No need to require ‘byebug’ on top of the page!) Run shotgun and execute the local host address in the browser, with ‘/juices’ at the end. If you look at your terminal, you would see that you’ve entered a byebug session.

Just like in pry, you can test your code in your terminal.

Upon entering the instance variable @juice, byebug has returned the array of all the juices. This is a way to inspect variable by printing. If you type help, you will be able to see all the list of available commands as well.

Differences?

  • The most obvious one is that Pry gives us highlighted syntax while byebug doesn’t.
  • Different commands
  • Byebug has its own useful commands such as “next.” Next steps byebug over to the next line within the same frame.
The arrow is pointing at line 5
“next” command skipped over line5, so we’re now on line 6.

Adding gems such as pry-debugger , pry-byebug or pry-navto Pry will also allow the use of byebug’s commands!

Conclusion

Use whatever comes most natural to you, whenever you need. Sometimes, the best way to detect an error is to look away, and get back to them after a well deserved break.

)
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