dan dan - 1 month ago 20
Ruby Question

How to check if the RUBY_VERSION is greater than a certain version?

Before I split the

RUBY_VERSION
string on a period and convert the bits into integers and so on, is there a simpler way to check from a Ruby program if the current
RUBY_VERSION
is greater than
X.X.X
?

Answer

Ruby's Gem library can do version number comparisons:

require 'rubygems' # not needed with Ruby 1.9+

ver1 = Gem::Version.new('1.8.7') # => #<Gem::Version "1.8.7">
ver2 = Gem::Version.new('1.9.2') # => #<Gem::Version "1.9.2">
ver1 <=> ver2 # => -1

See http://rubydoc.info/stdlib/rubygems/1.9.2/Gem/Version for more info.

Comments