Hirb在rails控制台中根本不起作用

时间:2013-01-23 06:35:22

标签: ruby-on-rails ruby console ruby-on-rails-3.2 hirb

我按照hirb rdoc上的教程进行操作,但遗憾的是,我的rails控制台根本无法正常工作。

我已经完成了sudo gem install hirb

并将hirb添加到我的Gemfile:

gem 'hirb', '~>0.7.0'

然后我发起了bundle install

我得到了这个结果:

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

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:6)

如果您使用pry作为rails控制台...将其添加到.pryrc文件中

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

答案 1 :(得分:2)

Yoshdog的回答已过时 - 它返回错误:

  

输出错误:#NoMethodError:nil的未定义方法`pager':NilClass

您可以使用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

这也可以让你启用/禁用Hirb,这可能会派上用场。

答案 2 :(得分:0)

如果您使用pry对我有用

$ pwd
/Users/me/path/rails-app
$ ls -la
-rw-r--r--   1 ryosuke  staff       554 12 26 17:50 .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