Search code examples
ruby-on-railsrubyrubygemsgemfile

Why does a Gem provide ~> AND => in RubyGems?


The gem for bcrypt at https://rubygems.org/gems/bcrypt/versions/3.1.11

shows this usage

gem 'bcrypt', '~> 3.1', '>= 3.1.11'

Why have the two versions of the pessimistic operator ?

We normally use just one version for other gems


Solution

  • >= 3.1.11 is an “optimistic” version constraint. It’s saying that all changes from 3.1.11 on will work, but for version 4.0.0 this will not be true.

    ~> 3.1 is “pessimistic”. This explicitly excludes the version that might break your code. It is basically saying >= 3.1 and < 4.0. But if you had ~> 3.1.1, it will be equal to >= 3.1.1 but less than 3.2

    If you want to allow use of newer backwards-compatible versions but need a specific bug fix you can use a compound requirement like '~> 3.1', '>= 3.1.11' This is detailed at http://guides.rubygems.org/patterns/#pessimistic-version-constraint If you want to allow use of newer backwards-compatible versions but need a specific bug fix you can use a compound requirement such as... '~> 2.2', '>= 2.2.1'