Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out whether a gem is installed in the system or not?

Tags:

ruby

rubygems

gem

How do I find out whether a gem is installed in the system or not?

%x('gem' 'list').split.find{|i| i == "capybara"}

Is there a shorter method?

like image 599
pankajdoharey Avatar asked Nov 30 '22 03:11

pankajdoharey


1 Answers

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

Gem::Specification::find_by_name('capybara')

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:

if Gem::Specification::find_all_by_name('capybara').any?
  # Gem is available
end
like image 91
Peter Brown Avatar answered Dec 07 '22 00:12

Peter Brown