Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ~> and >= when specifying rubygem in Gemfile?

People also ask

What is ~> Gemfile?

A Gemfile is a file that is created to describe the gem dependencies required to run a Ruby program. A Gemfile should always be placed in the root of the project directory.

How do I specify a Gemfile version?

There are several ways to specify gem versions: Use a specific version: gem "name-of-gem", "1.0" . You can find specific versions on Rubygems.org (provided that's the source you”re using) by searching for your gem and looking at the “Versions” listed. Use a version operator: gem "name-of-gem", ">1.0" .

What is the difference between Gemfile and Gemfile lock?

The Gemfile is where you specify which gems you want to use, and lets you specify which versions. The Gemfile. lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.

How do I read a Gemfile file?

The top page of the official rubygems site has a search box so just enter the name you want to know the feature of. That guides you to the search result page and you'll soon find a link to that gem. In this example you'll reach https://rubygems.org/gems/nokogiri.


That's a pessimistic version constraint. RubyGems will increment the last digit in the version provided and use that until it reaches a maximum version. So ~>0.8.5 is semantically equivalent to:

gem "cucumber", ">=0.8.5", "<0.9.0"

The easy way to think about it is that you're okay with the last digit incrementing to some arbitrary value, but the ones preceding it in the string cannot be greater than what you provided. Thus for ~>0.8.5, any value is acceptable for the third digit (the 5) provided that it is greater than or equal to 5, but the leading 0.8 must be "0.8".

You might do this, for example, if you think that the 0.9 version is going to implement some breaking changes, but you know the entire 0.8.x release series is just bugfixes.

However, simply using ">=0.8.5" would indicate that any version later than (or equal to) 0.8.5 is acceptable. There is no upper bound.


@millisami You can even use to add dependencies with the gemspec using the pessimistic constraint like this:

gem.add_runtime_dependency "thor", "~> 0.18.1"

If you don't know much about gem development or are just getting into it, these are some good references:

  1. Tutorial that teaches you how to make your own RubyGem, the standard practices associated with it, and how to upload it so that others can install it.
  2. How to create a Gem from scratch with Bundler