我现在有这个脚本。
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?
答案 0 :(得分:9)
我没有尝试,但我认为您可能正在寻找Gem.clear_paths
重置目录和路径值。下次请求目录或路径时,将从头开始计算值。这主要用于单元测试以提供测试隔离。
答案 1 :(得分:3)
您可以致电exec('irb')
答案 2 :(得分:0)
只需从'$“'中删除该文件:
require 'erb' # Loaded.
require 'erb' # Does nothing.
$".delete_if {|e| e =~ /erb\.(?:rb|so)/} # Remove "erb" from loaded libraies.
require 'erb' # Reloaded (with warnings if the first require was successful).