Countless hours spent spaghetti coding to work out your bugs, unexpected inputs, and flat out utter application inferno… Let’s face it debugging is something you’re going to have to be doing for the rest of your life as a programmer. The sooner you learn the tools that help you find the bugs in your code, the better off you’ll be in the long run. lucky for Rubyist there are many fancy debugging tools at our disposal. As programmers, we often have to mentally run code. We need to imagine how certain lines of code will behave given certain inputs. This can be daunting even for experienced devs, for beginners even more so. For starters we will go over Pry or binding.pry and why it’s an invaluable tool for jr and experienced devs alike.
What is Pry? pry is a data explorer, REPL stands for Read, Evaluate, Print, Loop, Pry is a powerful shell alternative to IRB (interactive ruby shell)pry is a debugging tool that freezes your code, allows you to pause your code at a specified line as you’re running it; at that specific moment, you have access to the methods and variables that come before.
in-app directory: gem install pry
aren't sure if you have it or not it's ok to give it a go anyway as gems will just update or overwrite…
After install, require it in our environment with the line:
Lastly, add binding.pry on the line we want our code to pause in.
now we're in like an interactive IRB session….pretty cool right. you can explore objects you at variables pry has a complete list of shell commands (more below) you can even delve into objects…You’ll be happy to know that the core Pry commands work with any of the interpreters, including JRuby and Rubinius.
Knowing the when’s and where’s of your code can help the debugging process immensely, and Pry gives us a quick and easy way to do just that. I hope you found these examples helpful…
Pry Cheat Sheet
pry -r ./config/app_init_file.rb- load your app into a pry session (look at the file loaded by config.ru)
pry -r ./config/environment.rb- load your rails into a pry session
help ls-- Display command options for pry command ls
ls <Object>-- Show all of the available methods that can be called by an object
_-- Last eval
? <Object>-- Shows more information (doc) about an object, or method
_file_-- Represent the last file Pry touched
wtf?-- Print the stack trace, same as
$-- Show source, shortcut for show-source
edit Class-- Open file in $EDITOR
edit Class#instance_method-- Open file in $EDITOR
<ctrl+r>-- Search history
_out_-- Array of all outputs values, also
cd <var>-- Step into an object, change the value of self
cd ..-- Take out of a level
edit --ex-- Edit the file where the last exception was thrown
.<Shell>-- Runs the command
whereami-- Print the context where the debugger is stopped
whereami 20-- Print the context 20 lines where the debugger is stopped
;-- Would mute the return output by Ruby
play -l-- Execute the line in the current debugging context
next-- execute next line
step-- step into next function call
continue-- continue through stack
rescue rspec-- break on exception in rspec
rescue rails server-- break on exception in rails server
try-again-- run last failing spec, reloads the file not the enviornment
Other useful tools while debugging with rails application environment can help you save countless hours debegging your code. Here are some of the most popular available ruby gems…
gem install byebugdef some_methodsome code
byebug # write it in your code and run the method or programsome other code
Byebug is a simple to use and feature rich debugger for Ruby. It uses the TracePoint API for execution control and the Debug Inspector API for call stack navigation. Therefore, Byebug doesn’t depend on internal core sources. Byebug is also fast because it is developed as a C extension and reliable because it is supported by a full test suite.
The debugger permits the ability to understand what is going on inside a Ruby program while it executes and offers many of the traditional debugging features such as:
- Stepping: Running your program one line at a time.
- Breaking: Pausing the program at some event or specified instruction, to examine the current state.
- Evaluating: Basic REPL functionality, although pry does a better job at that.
- Tracking: Keeping track of the different values of your variables or the different lines executed by your program.
(help) inside of the ByeBug terminal:
Unlike binding.pry, that is inside of your code and freezez the application where it’s at with no added functionality, ByeBug has some addtional functionality within the terminal console.
- n => this executes the next line of code. This will not step into a function however. It will just execute the function and move to the next line after the function.
- s => this will step into the next stack of code. Which means this will step the next function to be ran.
- c => this continues the program until it either concludes or it reaches another breakpoint.
- l => this outputs the source code that is currently being ran.
- q => this will quit the ByeBug and return back to the program.
- irb => this will put you into an interactive ruby session.
- save => this will save your ByeBug history either with a specified file name inside of an argument, or with a default .byebug_save file name.
- f => this shows the information for the current execution stack. It can also take in an integer as an argument to see what will be executing next.
- hist => this will show you all of the history and persists through all of your debugging sessions.
Additional rails debbuging tools:
debug helper will return a <pre> tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:
to_yaml — Alternatively, calling
to_yaml on any object converts it to YAML. You can pass this converted object into the
simple_format helper method to format the output. This is how
debug does its magic.
inspect — Another useful method for displaying object values is
inspect, especially when working with arrays or hashes. This will print the object value as a string. For example: