Cool tricks to do with Ruby, Rails and Pry Part 2: Binding, Awesome Print, Source Edit

10 December 2016 on . 3 minutes to read

As helpful as George Carlin in Bill and Ted's Excellent Adventure

Part 2 is here with a few enhancements I use in my day to day development, researching and code audits for Ruby and Rails. Check out the first part as well; there’s always more to learn.

binding.pry and binding.irb

Insert binding.pry into your codebase, and you’ll be dropped into a debugging pry session at that line of code. I use this all the time, especially for rspec testing when I can’t quickly identify why a spec’s not passing.

binding.irb will be available with Ruby 2.4

awesome print

Speed up console hacking with visual organization. Key sorting and colors for different object types makes investiation and identification much quicker.

AwesomePrint.defaults = {
  :indent => -2,
  sort_keys: true,
  :color => {
    :args       => :yellowish,
    :array      => :blue,
    :bigdecimal => :blue,
    :class      => :yellow,
    :date       => :greenish,
    :falseclass => :red,
    :fixnum     => :blue,
    :float      => :blue,
    :hash       => :cyanish,
    :keyword    => :cyan,
    :method     => :purpleish,
    :nilclass   => :red,
    :rational   => :blue,
    :string     => :yellowish,
    :struct     => :pale,
    :symbol     => :cyanish,
    :time       => :greenish,
    :trueclass  => :green,
    :variable   => :cyanish
require 'awesome_print'
require 'awesome_print'


awesome print output

MUCH easier to look through than having it all smashed together.

Easily Find and Edit Source Code

Need to quickly edit some code? Make sure your default editor is set with:

export EDITOR='subl -w' #or your editor of choice

And then edit Object from a pry session. Object can be any class name; it will open up the source file in your editor for you to view the code. Super handy for testing quick alterations to gem classes rather than typing bundle which gem and going from there.

More to Come

I’ll put out more of these posts as time goes on and I identify snippets which make life easier. Hit me up with suggestions if you have any; I’d love to see and include them in future posts!

If you enjoy having free time and the peace of mind that a professional is on your side, then you’d love to have me work on your project.

Contact or view a list of available services to see how I’ll make your life better, easier and bring satisfaction back into you running your business.