1

N.B.

  1. I'm not trying to do this from the command line.
  2. None of these answers include all the gems, so nothing based on these solutions will work
  3. I'm using Bundler (and Rails, but that should not matter).

I'm only interested in getting a particular (locally installed) gem, but if you have a solution that lists all gems installed in the current app's bundle (which the aforementioned link fails to show how to do, despite one answer purporting to do so) I can deal with filtering out the ones I don't care about.

So, from within Ruby code, how can I check the version of a gem available to the current app?

iconoclast
  • 21,213
  • 15
  • 102
  • 138
  • This is confusing. It could mean the latest version of a gem on RubyGems.org, the latest version installed on the local machine, or the version currently loaded by the currently running program. – Peter Camilleri Feb 09 '19 at 21:51
  • @PeterCamilleri: you're right that I could have made it immediately clear without any sleuthing required, but the sleuthing is pretty minimal. (E.g. bundler is only relevant in the case of the local gems installed in the bundle used by the current app or script. The linked answers are all regarding locally installed gems, but they fail to list _all_ the gems. The command line makes it very easy and straightforward to see locally installed gems, but not to scrape rubygems.org! And I didn't mention scraping anywhere in the question. Nonetheless:) I take your point & will clarify the question. – iconoclast Feb 10 '19 at 17:55

1 Answers1

4

You can try this:

puts Gem.loaded_specs["gem_name"].version
hashrocket
  • 2,210
  • 1
  • 16
  • 25