在 irb 中重新加载 rubygems?
我现在有这个脚本。
def r(this)
require this
puts "#{this} is now loaded."
rescue LoadError
puts "The gem '#{this}' is missing."
puts "Should I install it? [y/n]"
data = gets
if data =~ /yes|y/i
puts "Installing #{this}, hold on."
if `gem install #{this}` =~ /Successfully/i
load this
end
else
puts "Okey, goodbye."
end
end
这使得动态需要库成为可能。 像这样:r“haml”
。
问题是安装后我无法加载gem。 使用 load this
或 load File.expand_path("~/.irbrc")
不起作用。
这是一个例子。
>> r "absolutize"
The gem 'absolutize' is missing.
Should I install it? [y/n]
y
Installing absolutize, hold on
LoadError: no such file to load -- absolutize
>> require "absolutize"
LoadError: no such file to load -- absolutize
>> exit
$ irb
>> require "absolutize"
=> true
有没有办法即时重新加载 rubygems 或 irb?
I've this script right now.
def r(this)
require this
puts "#{this} is now loaded."
rescue LoadError
puts "The gem '#{this}' is missing."
puts "Should I install it? [y/n]"
data = gets
if data =~ /yes|y/i
puts "Installing #{this}, hold on."
if `gem install #{this}` =~ /Successfully/i
load this
end
else
puts "Okey, goodbye."
end
end
That makes it possible to require libs on the fly.
Like this: r "haml"
.
The problem is that I can't load the gem after it has been installed.
Using load this
or load File.expand_path("~/.irbrc")
does not work.
Here is an example.
>> r "absolutize"
The gem 'absolutize' is missing.
Should I install it? [y/n]
y
Installing absolutize, hold on
LoadError: no such file to load -- absolutize
>> require "absolutize"
LoadError: no such file to load -- absolutize
>> exit
$ irb
>> require "absolutize"
=> true
Is there a way to reload rubygems or irb on the fly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有尝试,但我认为您可能正在寻找
Gem.clear_paths
I did not try, but I think you might be looking for
Gem.clear_paths
您可以通过调用
exec('irb')
重置irbYou can reset irb by calling
exec('irb')
只需从 '$"' 中删除该文件:
请参阅 http://www.zenspider。 com/Languages/Ruby/QuickRef.html#19
Just remove the file from ´$"´:
See http://www.zenspider.com/Languages/Ruby/QuickRef.html#19