1

我按照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

有人可以帮忙吗?

4

3 回答 3

8

如果您使用 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
于 2014-08-13T12:43:03.843 回答
2

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

输出错误:# NoMethodError: undefined method `pager' for nil:NilClass

您可以使用文档中的更新代码来解决此问题:

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,这可能会派上用场。

于 2015-10-20T09:40:52.553 回答
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
于 2018-12-26T08:54:04.713 回答