在 irb 中重新加载 ruby​​gems?

发布于 2024-11-03 19:31:55 字数 917 浏览 0 评论 0原文

我现在有这个脚本。

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 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

有没有办法即时重新加载 ruby​​gems 或 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2024-11-10 19:31:55

我没有尝试,但我认为您可能正在寻找 Gem.clear_paths

重置目录和路径值。下次请求 dir 或 path 时,将从头开始计算这些值。这主要由单元测试用来提供测试隔离。

I did not try, but I think you might be looking for Gem.clear_paths

Reset the dir and path values. The next time dir or path is requested, the values will be calculated from scratch. This is mainly used by the unit tests to provide test isolation.

绅士风度i 2024-11-10 19:31:55

您可以通过调用exec('irb')重置irb

You can reset irb by calling exec('irb')

心房的律动 2024-11-10 19:31:55

只需从 '$"' 中删除该文件:

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

Just remove the file from ´$"´:

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).

See http://www.zenspider.com/Languages/Ruby/QuickRef.html#19

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文