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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With