在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?
我没试过,但我想你可能正在寻找Gem.clear_paths
重置目录和路径值。 下次请求目录或路径时,将从头开始计算值。 这主要用于unit testing以提供测试隔离。
您可以通过调用exec('irb')
来重置irb
只需从’$“’中删除该文件:
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).
请参阅http://www.zenspider.com/Languages/Ruby/QuickRef.html#19