在irb中重新加载rubygems?

Lin*_*der 9 ruby rubygems irb

我现在有这个脚本.

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
Run Code Online (Sandbox Code Playgroud)

这使得可以动态地需要库.像这样:r "haml".

问题是我无法在安装后加载gem.使用load thisload 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
Run Code Online (Sandbox Code Playgroud)

有没有办法在飞行中重新加载rubygems或irb?

Ama*_*dan 9

我没试过,但我想你可能在寻找 Gem.clear_paths

重置目录和路径值.下次请求目录或路径时,将从头开始计算值.这主要用于单元测试以提供测试隔离.