Debugging with Pry
Alright, let us pry away your pride.
What is pry? It is one of many useful tools in Ruby. It helps the programmer to debug the code.
What is debugging? It’s the process of reading over the code, line by line and figuring out where something went wrong.
The word debug literally means as it sound. The word bug and debug was popularized due to Admiral Grace Hopper, when she discovered a moth stuck in a Mark II computer. It was the source, that was causing the computer to slow down. When she removed the moth, she debugged and resolved the issue.
How does pry work? Pry is a gem, that is needed to be installed first.
gem install pry
After its been install, on the very first line of the code type in
require “pry”
By having require “pry” at the very first line of the code. All allows Pry to gain access to the code.
To invoke Pry we simply type
binding.pry
anywhere before the issue or bug. Next invoke the method or function where binding.pry is placed. If Pry runs the code, it’s working, it hasn’t reached the bug yet. If Pry won’t run, then that section is where it has the bug. Simple move the binding.pry up some lines and if it run, the world will stop. It nows time to hunt some bugs.
When Pry is invoked, the code will stop and everything freezes. It will stop you at the line where you place binding.pry. Everything above binding.pry has ran and everything below hasn’t yet
Notice how we get a new terminal prompt that said pry(main). This is just another REPL (read, evaluate, print, loop) just like ruby IRB. This is where we play around with our code. It lets us know what variables and values we are current working with.
Go and play around with pry and discover all sorts of new bugs.