简体   繁体   中英

Finding the latest version of a gem in ruby script

I want to find out latest version of Nokogiri. For this I am using:

gem list nokogiri --remote

However this returns a long list of gems having "nokogiri" in it:

backupify-rsolr-nokogiri (0.12.1.1)
epp-nokogiri (1.0.0)
glebm-nokogiri (1.4.2.1)
jwagener-nokogiri (1.4.1)
nokogiri (1.6.3.1 ruby java x64-mingw32 x86-mingw32, 1.6.1 x86-mswin32-60, 1.4.4.1 x86-mswin32)
nokogiri-diff (0.2.0)
nokogiri-fitzsimmons (1.5.5.3 ruby java)
nokogiri-happymapper (0.5.9)
nokogiri-happymapper-deiga (0.5.10)
nokogiri-maglev- (1.5.5.20120817130721)
nokogiri-maven (1.5.0 java)
nokogiri-plist (0.5.0)
nokogiri-pretty (0.1.0)
nokogiri-streaming-reader (0.0.2)
nokogiri-styles (0.1.2)
nokogiri-xmlsec (0.0.4)
nokogiri-xmlsec1 (0.0.7)
nokogiri_bang_finders (1.0.0)
nokogiri_helper (0.0.1)
nokogiri_html_helpers (0.1.4)
nokogiri_truncate_html (0.0.3)
rack-nokogiri (0.1.0)
revo-nokogiri (1.4.1 java)
rsolr-nokogiri (0.0.0)
rss-nokogiri (0.0.1.1)
rubyjedi-nokogiri_java (1.4.0.20100513161003 java)
sax-machine-nokogiri-1.4.4-safe (0.0.15)
spp_nokogiri_ext (0.0.5)
superfeedr-nokogiri (1.4.0.20091116183308)
tenderlove-nokogiri (0.0.0.20081001111445, 0.0.0 x86-mswin32-60)
watir-nokogiri (1.0.0)

Is there a command to just get the Nokogiri gem from it? Ie:

nokogiri (1.6.3.1 ruby java x64-mingw32 x86-mingw32, 1.6.1 x86-mswin32-60, 1.4.4.1 x86-mswin32)

Also how can I make a Ruby script run this command? I am trying to write a script which will find the latest version of the gem and then perform some actions. My script would be responsible to run this command.

$ gem list "^nokogiri$" --remote

*** REMOTE GEMS ***

nokogiri (1.6.3.1 ruby java x64-mingw32 x86-mingw32, 1.6.1 x86-mswin32-60, 1.4.4.1 x86-mswin32)

The help for both list and search state they're for local and remote gems respectively; it might be better to use search . It has the same regex functionality.

Neither help mentions the regex capability; I intuited it from the results of "nokogiri".

I missed the "Ruby Script" part, in which case you're far better off just using the existing Gem functionality, nothing else makes any real sense. It's a good habit to scan the libraries of the tools you're using since they obviously already have the functionality built in.

If you need this inside a Ruby script, you may want to take a look at the class Gem::SpecFetcher used by the gem command to perform remote searches, for example:

specs = Gem::SpecFetcher.fetcher.detect(:latest) do |name_tuple|
  name_tuple.name == 'nokogiri' && name_tuple.platform == 'ruby'
end

specs is an array (with just on element in this case) of 2-element arrays, the first being a Gem::NameTuple object and the second being a Gem::Source object (we are not interested in it here).

found = specs.first.first 
# => #<Gem::NameTuple nokogiri, 1.6.3.1, ruby>
found.name 
# => "nokogiri"
found.version 
# => #<Gem::Version "1.6.3.1">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM