Screen Shot 2016-03-21 at 12.29.33

I love Pry, but the output sometimes can be hard to read. After digging in the web I learned about Hirb.

Hirb provides a mini view framework for console applications and uses it to improve ripl(irb)’s default inspect output.

1. Add Hirb to your Gemfile

gem 'hirb', '~> 0.7.3'

Run bundle install

2. Edit your .pryrc file:

First search for .pryrc in your root directory if the file can’t be found then create one:

→ cd
→ ls .pryrc
→ ls: .pryrc: No such file or directory
→ touch ~/.pryrc
→ open ~/.pryrc

Paste this in .pryrc:

begin
  require 'hirb'
rescue LoadError
  # Missing goodies, bummer
end

if defined? Hirb
  # Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
  Hirb::View.instance_eval do
    def enable_output_method
      @output_method = true
      @old_print = Pry.config.print
      Pry.config.print = proc do |*args|
        Hirb::View.view_or_page_output(args[1]) || @old_print.call(*args)
      end
    end

    def disable_output_method
      Pry.config.print = @old_print
      @output_method = nil
    end
  end

  Hirb.enable
end



That’s it, every time you load your Rails Console, Hirb will be enabled. You can disable it with Hirb.disable and reenable it with… you guess it: Hirb.enable.

Join the conversation! 1 Comment

  1. Nice. Thanks for posting this. I’m really like Hirb and am happy to see a way to add it to pry.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

About ivor

I'm a Front-end Developer working at Envato as a Quality Senior Reviewer @ ThemeForest.

Category

Command Line, Rails, Ruby