1

I followed the tutorial on hirb rdoc but unfortunately, my rails console is not working at all.

I've already done sudo gem install hirb

and added hirb to my Gemfile:

gem 'hirb', '~>0.7.0'

Then I launched bundle install

And I get this result :

rails c
Loading development environment (Rails 3.2.11)
> require 'hirb'
=> false
> Hirb.enable
=> true
> Municipality.all
Municipality Load (0.8ms)  SELECT "municipalities".* FROM "municipalities" ORDER BY name asc
=> [#<Municipality id: 1, district_id: 6, name: "Ambalamanasy II", created_at: "2013-01-16 12:11:45", updated_at: "2013-01-16 12:11:45">,
...
# doesn't work

Could anyone help?

davegson
  • 8,205
  • 4
  • 51
  • 71
jramby
  • 426
  • 5
  • 14

3 Answers3

8

If your using pry as your rails console... add this in your .pryrc file

require 'hirb'

Hirb.enable

old_print = Pry.config.print
Pry.config.print = proc do |output, value|
  Hirb::View.view_or_page_output(value) || old_print.call(output, value)
end
yoshdog
  • 133
  • 1
  • 7
3

Yoshdog's answer is outdated - it returns an error:

output error: # NoMethodError: undefined method `pager' for nil:NilClass

You can fix this by using the updated code from the docs:

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

This will also allow you to enable/disable Hirb, which may come in handy.

davegson
  • 8,205
  • 4
  • 51
  • 71
  • Thanks so much for the update. This works beautifully. – Sia Jun 02 '16 at 20:01
  • @TheChamp, this gives formatting in pager structure, how can we change to tabular form ? Any help will be heartily appreciated. Thank you. – codemilan Feb 20 '18 at 08:07
0

If you using pry it's works for me

$ pwd
/Users/me/path/rails-app
$ ls -la
-rw-r--r--   1 ryosuke  staff       554 12 26 17:50 .pryrc

and

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
Ryosuke Hujisawa
  • 2,682
  • 1
  • 17
  • 18