I needed to compare pessimistic version constraints a while back, but I didn't want to depend on RubyGems to do it, so I wrote a simple Version class that does everything I need: shorts.jeffkreeftmeijer.com/2014/…
– jkreeftmeijerOct 16 '14 at 6:13

8 Answers
8

The Gem::Version... syntax made me thought I would need to install a gem. But it was not required.
– GuillaumeOct 17 '12 at 17:08

Note: This gives an error about undefined variable 'Gem' for me on Ruby 1.x, but works as expected on Ruby 2.x. In my case I was checking RUBY_VERSION against being Ruby 1.x (not 2.x), so I just did RUBY_VERSION.split('.')[0] == "1" like John Hyland and DigitalRoss do it.
– uliwitnessJun 19 '13 at 15:10

Like some of the other answers here, it looks like you're doing string comparisons instead of numerical, which will cause problems when comparing versions like '0.10' and '0.4'.
– John HylandJan 13 '10 at 17:00

I have seen that, but require me to use 2 gems to do a really simple thing. I want to use that as last choice.
– user239895Jan 12 '10 at 18:16

8

"Don't reinvent the wheel". Because it's simple doesn't mean the programmer didn't put work and thought into it. Use the gem, read the code, and learn from it - and move on to bigger and better things!
– TrevokeJan 13 '10 at 20:32

Dependency management and version maintenance is a difficult problem, probably much more difficult than the task of comparing 2 versions. I totally agree that introducing 2 more dependencies should be a last resort in this case.
– kkodevFeb 16 '17 at 14:49

Essentially, you turn each version string in to an array of integers and then use the array comparison operator. You could break out the component steps to get something a little easier to follow if this is going in code somebody will need to maintain.