[英]Permanently add a directory to Ruby $LOAD_PATH
I'd like the option to use awesome_print in every IRB console or Rails console.我想要在每个 IRB 控制台或 Rails 控制台中使用awesome_print的选项。
The IRB console is pretty much working satisfactorily right now. IRB 控制台现在几乎可以令人满意地工作。 If I run
irb
, I can type require 'awesome_print'
and it works.如果我运行
irb
,我可以输入require 'awesome_print'
并且它可以工作。
The Rails console isn't as easy. Rails 控制台并没有那么简单。
require 'awesome_print'
doesn't work. require 'awesome_print'
不起作用。 I apparently have to do this:我显然必须这样做:
> $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib'
After that, require 'awesome_print'
works fine.之后,
require 'awesome_print'
工作正常。
But I definitely don't want to have to type $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib'
and then require 'awesome_print'
every single time I open a Rails console just to be able to use awesome_print
.但我绝对不想输入
$LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib'
然后每次打开都require 'awesome_print'
Rails 控制台只是为了能够使用awesome_print
。 That seems ridiculous.这看起来很荒谬。
So, how can I permanently add a path to Ruby's $LOAD_PATH
?那么,如何永久添加到 Ruby 的
$LOAD_PATH
的路径?
Note: I don't want to add awesome_print
to the Gemfile
of any particular project.注:我不想添加
awesome_print
到Gemfile
任何特定项目。 I want awesome_print
to be available to all my Ruby/Rails projects.我希望
awesome_print
可用于我所有的Ruby/Rails 项目。
You could simply use aa ~/.irbrc
file and do:您可以简单地使用 aa
~/.irbrc
文件并执行以下操作:
require 'awesome_print'
Now, open up another IRB prompt:现在,打开另一个 IRB 提示:
irb(main):003:0> ap hash
{
"a" => "b"
}
Edit: this isn't working in rails, seems to be a known issue .编辑:这在 rails 中不起作用,似乎是一个已知问题。
puts the following to the .irbrc
:将以下内容放入
.irbrc
:
to_load = %w[
awesome_print
coderay
hirb
pry
pry-doc
pry-remote
pry-theme
slop
yard
].join('|')
regexp = Regexp.new( "(#{to_load})" )
Gem.path.each do |path|
Dir.new("#{path}/gems").each do |gem_path|
next if %w[ . .. ].any?{ |d| gem_path == d }
new_el = "#{path}/gems/#{gem_path}/lib"
$LOAD_PATH << new_el if new_el =~ regexp
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.