[英]Hirb doesn't work at all in rails console
I followed the tutorial on hirb rdoc but unfortunately, my rails console is not working at all. 我按照hirb rdoc上的教程进行操作,但是不幸的是,我的Rails控制台根本无法正常工作。
I've already done sudo gem install hirb
我已经完成了sudo gem install hirb
and added hirb to my Gemfile: 并将hirb添加到我的Gemfile中:
gem 'hirb', '~>0.7.0'
Then I launched bundle install
然后我启动了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? 有人可以帮忙吗?
If your using pry as your rails console... add this in your .pryrc file 如果您使用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
Yoshdog's answer is outdated - it returns an error: Yoshdog的答案已过时-返回错误:
output error: # NoMethodError: undefined method `pager' for nil:NilClass 输出错误:#NoMethodError:nil:NilClass的未定义方法`pager'
You can fix this by using the updated code from the docs : 您可以使用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. 这也将允许您启用/禁用Hirb,这可能会派上用场。
If you using pry
it's works for me 如果您使用pry
对我pry
$ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.