ruby: code to install gem if missing

本秂侑毒 提交于 2019-12-12 09:39:43

问题


is there some ruby code I can use to install a gem from a local file, if that gem is not installed?

i'm thinking it would look something like:

if !gem_installed("some gem name")
  system "gem install -l local_copy.gem"
end

i don't know if anything exists that lets me check for gems like this or not...


回答1:


Checking availability is covered in this previous StackOverflow Quesiton

begin
  gem "somegem"
  # with requirements
  gem "somegem", ">=2.0"
rescue Gem::LoadError
  # not installed
end

or

matches = Gem.source_index.find_name(gem.name, gem.version_requirements)

As for the install, it looks like rails uses the system for gem install also

 puts %x(#{cmd})


来源:https://stackoverflow.com/questions/1633087/ruby-code-to-install-gem-if-missing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!