Hirb doesn't work at all in rails console

I followed the hirb rdoc tutorial , but unfortunately my rails console doesn't work at all.

I did it already sudo gem install hirb

and added hirb to my gemfile:

gem 'hirb', '~>0.7.0'

Then i ran 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

Can anyone help?

+1
source share
2 answers

If you use pry as a rails console ... add this to 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
+6
source

The answer to Yoshdog is deprecated - it returns an error:

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

You can fix this using the updated code from the documentation :

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

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

+1

Source: https://habr.com/ru/post/1783007/


All Articles