How do I find out whether a gem is installed in the system or not?
如何确定系统中是否安装了gem?
%x('gem' 'list').split.find{|i| i == "capybara"}
Is there a shorter method?
有更短的方法吗?
4 个解决方案
#1
15
If you're trying to do this from within ruby, you can use a built-in RubyGem method. Older versions provide a Gem.available?('capybara')
method that returns a boolean, but this has been deprecated. The recommended way now is to use (assuming you're using a version that supports it):
如果您尝试在ruby中执行此操作,则可以使用内置的RubyGem方法。旧版本提供了一个返回布尔值的Gem.available?('capybara')方法,但这已被弃用。现在推荐的方法是使用(假设您使用的是支持它的版本):
Gem::Specification::find_by_name('capybara')
http://rubygems.rubyforge.org/rubygems-update/Gem/Specification.html
http://rubygems.rubyforge.org/rubygems-update/Gem/Specification.html
Update
更新
If you want a boolean result, you could use .find_all_by_name()
and check if the resulting array is empty:
如果需要布尔结果,可以使用.find_all_by_name()并检查结果数组是否为空:
if Gem::Specification::find_all_by_name('capybara').any?
# Gem is available
end
#2
3
%x('gem' 'list' | 'grep' 'capybara').empty?
#3
3
I stick this at the beginning of my Gemfile:
我坚持在我的Gemfile的开头:
def gem_available?(gemname)
if Gem::Specification.methods.include?(:find_all_by_name)
not Gem::Specification.find_all_by_name(gemname).empty?
else
Gem.available?(gemname)
end
end
then just use:
然后使用:
if (gem_available?('gem_i_need'))
and everything works nicely!
一切都很好!
#4
1
Here's some code that works for me. It also properly handles the Gem::LoadError
that gets thrown when you try to load a gem that could not be found.
这里有一些适合我的代码。它还可以正确处理当您尝试加载无法找到的gem时抛出的Gem :: LoadError。
require 'rubygems'
def can_we_find_gem(gem_name)
found_gem = false
begin
found_gem = Gem::Specification.find_by_name(gem_name)
rescue Gem::LoadError
puts "Could not find gem '#{gem_name}'"
else
puts "Found gem '#{gem_name}'"
end
end
can_we_find_gem('chef')
can_we_find_gem('not-chef')
#1
15
If you're trying to do this from within ruby, you can use a built-in RubyGem method. Older versions provide a Gem.available?('capybara')
method that returns a boolean, but this has been deprecated. The recommended way now is to use (assuming you're using a version that supports it):
如果您尝试在ruby中执行此操作,则可以使用内置的RubyGem方法。旧版本提供了一个返回布尔值的Gem.available?('capybara')方法,但这已被弃用。现在推荐的方法是使用(假设您使用的是支持它的版本):
Gem::Specification::find_by_name('capybara')
http://rubygems.rubyforge.org/rubygems-update/Gem/Specification.html
http://rubygems.rubyforge.org/rubygems-update/Gem/Specification.html
Update
更新
If you want a boolean result, you could use .find_all_by_name()
and check if the resulting array is empty:
如果需要布尔结果,可以使用.find_all_by_name()并检查结果数组是否为空:
if Gem::Specification::find_all_by_name('capybara').any?
# Gem is available
end
#2
3
%x('gem' 'list' | 'grep' 'capybara').empty?
#3
3
I stick this at the beginning of my Gemfile:
我坚持在我的Gemfile的开头:
def gem_available?(gemname)
if Gem::Specification.methods.include?(:find_all_by_name)
not Gem::Specification.find_all_by_name(gemname).empty?
else
Gem.available?(gemname)
end
end
then just use:
然后使用:
if (gem_available?('gem_i_need'))
and everything works nicely!
一切都很好!
#4
1
Here's some code that works for me. It also properly handles the Gem::LoadError
that gets thrown when you try to load a gem that could not be found.
这里有一些适合我的代码。它还可以正确处理当您尝试加载无法找到的gem时抛出的Gem :: LoadError。
require 'rubygems'
def can_we_find_gem(gem_name)
found_gem = false
begin
found_gem = Gem::Specification.find_by_name(gem_name)
rescue Gem::LoadError
puts "Could not find gem '#{gem_name}'"
else
puts "Found gem '#{gem_name}'"
end
end
can_we_find_gem('chef')
can_we_find_gem('not-chef')