What does tilde-greater-than (~>) mean in Ruby gem dependencies?

RubyRubygems

Ruby Problem Overview


What does ~> mean in the context of Ruby gem depenedencies?

For example, when opening a legacy project in the RubyMine IDE, I get this message

Gems required for project are not attached:
arel (> 2.0.2),
rspec-expectation (> 2.5.0)...

I've seen this tilde-greater-than notation elsewhere in the Ruby world (it's not specific to RubyMine). Does this operator have a name other than the awkward-sounding tilde-greater-than?

Ruby Solutions


Solution 1 - Ruby

It means "equal to or greater than in the last digit", so e.g. ~> 2.3 means "equal to 2.3 or greater than 2.3, but less than 3.0", while ~> 2.3.0 would mean "equal to 2.3.0 or greater than 2.3.0, but less than 2.4.0".

You can pronounce it as "approximately greater than".

§ Pessimistic version constraint

Solution 2 - Ruby

it means bring any lower version equal or greater than, but not a major version.

So for example arel (~> 2.0.2), will use (if availble) versions

  • 2.0.2
  • 2.0.3
  • 2.0.? (as long as ? is >= 2)

but it won't use 2.1.?

Solution 3 - Ruby

According to the internet

> If a RubyGem dependency uses the syntax "~> 1.4.37", that means "a version greater than or equal to 1.4.37, but not 1.5 or higher." 1

In other words, for you
arel can be 2.1 > version >= 2.0.2 and
rspec-expectation can be 2.6 > version >= 2.5.0.

Solution 4 - Ruby

What this means is that you are expecting a gem that is version 2.0.2 or higher, but not 2.1 in the case of arel (~> 2.0.2) This is done since people are not supposed to release breaking syntax changes in minor revisions. So arel 2.0.3 would be expected to have bug/stability fixes over 2.0.2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjwfearnView Question on Stackoverflow
Solution 1 - Rubysepp2kView Answer on Stackoverflow
Solution 2 - RubyAugustoView Answer on Stackoverflow
Solution 3 - RubyashaysView Answer on Stackoverflow
Solution 4 - RubyMichael PapileView Answer on Stackoverflow