简体   繁体   中英

How can I install a gem from a Ruby script?

From a Ruby script, I want to send a command to install a gem if it is missing. Let's say it is "foo" . I thought that the command to check the existence of a gem were Gem.find_files and the command to install a gem were Gem.install . I tried this:

if Gem.find_files("foo").empty?
  Gem.install("foo")
end

and Gem.find_files seems to work, but Gem.install returns an error Uninitialized constant Gem::DependencyInstaller . Assuming that this is the first part of the code to be run, and I am working with a freshly installed Ruby with no gems installed at this point other than the core libraries that come with Ruby 2, what is the correct way to install the gems from Ruby script (not from the shell)?

How about:

require 'rubygems/dependency_installer.rb' 

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM