如何从 ruby 脚本中安装 rubygem 并在之后需要此 gem?
How to install a rubygem from inside a ruby script and require this gem afterwards?
我想做这样的事情:
begin
require 'progressbar'
rescue LoadError => e
puts "exception .. installing with gem"
h = system 'gem install progressbar'
puts "gem installed #{h}"
require 'progressbar'
end
pbar = ProgressBar.new("test", 100)
100.times {sleep(0.1); pbar.inc}; pbar.finish
但显然,如果您 运行 此代码段,则进度条 gem 不可用。
我找到了这个讨论:https://www.ruby-forum.com/topic/131346
显然调用 Gem.clear_paths
解决了这个问题。总计:
begin
require 'progressbar'
rescue LoadError => e
puts "exception .. installing gem"
h = system 'gem install progressbar'
puts "gem installed #{h}"
Gem.clear_paths
require 'progressbar'
end
pbar = ProgressBar.new("test", 100)
100.times {sleep(0.1); pbar.inc}; pbar.finish
我想做这样的事情:
begin
require 'progressbar'
rescue LoadError => e
puts "exception .. installing with gem"
h = system 'gem install progressbar'
puts "gem installed #{h}"
require 'progressbar'
end
pbar = ProgressBar.new("test", 100)
100.times {sleep(0.1); pbar.inc}; pbar.finish
但显然,如果您 运行 此代码段,则进度条 gem 不可用。
我找到了这个讨论:https://www.ruby-forum.com/topic/131346
显然调用 Gem.clear_paths
解决了这个问题。总计:
begin
require 'progressbar'
rescue LoadError => e
puts "exception .. installing gem"
h = system 'gem install progressbar'
puts "gem installed #{h}"
Gem.clear_paths
require 'progressbar'
end
pbar = ProgressBar.new("test", 100)
100.times {sleep(0.1); pbar.inc}; pbar.finish