what does ~> mean in gemspec dependencies? [duplicate] - ruby

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Meaning of tilde-greater-than (~>) in version requirement?
I see something like this a lot in gemspecs:
s.add_dependency 'some_gem', '~> 1.5.0'
What does ~> mean?

From gembundler
The specifier ~> has a special meaning, best shown by example.
~> 2.0.3 is identical to >= 2.0.3 and < 2.1.
~> 2.1 is identical to >= 2.1 and < 3.0.
~> 2.2.beta will match prerelease versions like 2.2.beta.12.

Related

Understanding the tilde operator in Gemfile.lock

I currently have a dependency of the following
i18n (~> 0.6, >= 0.6.4)
I've been having a read through of the Ruby Gems - Declaring Dependency Guide
And I've found out that the '~> 0.6' part means anything between 0.6 and 1.0 - correct me if that's wrong.
But I'm still confused, what does this actually mean with a second comma separated value?
A colleague believes it means
>= 0.6.4, <= 0.7
But I'm not so sure.
short answer: as pointed by Holger in the comments, ~> 0.6, >= 0.6.4 means >= 0.6.4 and < 1.0.
The ~> operator is called pessimistic operator (or twiddle-wakka), and its objective is to guard the gems from potential bugs/failures in future releases.
When you're building a gem, you must create a special Specification Class and put it in a .gemspec file or in a Rakefile. This class contains the information for the gem, like its name, version, license and the dependencies.
And is a good practice specify the dependencies following a pessimistic version constraint. Therefore, notations like ~> 0.6, >= 0.6.4 are very common.
You can find more information here.

Execute specific version of a gem [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How can I call an older version of a gem from the commandline?
If I have two versions of a gem, how do I choose which version to execute?
For example, if gem list heckle gives me
*** LOCAL GEMS ***
heckle (2.0.0.b1, 1.4.3)
and heckle --version from the command line gives me
heckle 2.0.0.b1
how can I tell it to run heckle 1.4.3 instead?
Would I need to use bundler in order to do that? If so, how much yak shaving would be involved?
You can easily do this with RubyGems.
For example:
require 'rubygems' # if RUBY_VERSION < 1.9
gem 'example', '1.2'
Further reading:
http://docs.rubygems.org/read/chapter/4#page71
But using of Bundler is already a better solution.

How to do an "OR" when requiring a Gem version

Currently I have a Gemspec that requires a specific version of Rails 3.x with Rails > 3.1:
Gem::Specification.new do |s|
# (...)
s.add_dependency "railties", "~> 3.1"
# (...)
end
I am looking to update this statement to require ~> 3.1 OR ~> 4.0.0. I had tried already:
~> 3.1, ~> 4.0.0
~> 3.1 OR ~> 4.0.0
~> 3.1 || ~> 4.0.0
~> 3.1 ~> 4.0.0
None have worked. Is that possible with the current RubyGems version?
For now I am using > 3.1, but the real question is if that's possible.
Since you are using ~>, it seems as if you are willing to use any version of the gem that is after 3.1, that is, any 3.x OR any 4.0.x, but not a 4.x where x is greater than 1.
If that is your intention, then according to the rubygems guide, you'll want to use a pattern like:
spec.add_dependency 'library', ['>= 3.1', '< 4.1']
I think it might be the addition of brackets that you need.

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

This question already has answers here:
Meaning of tilde-greater-than (~>) in version requirement?
(4 answers)
Closed 8 years ago.
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?
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
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.?
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.
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

What does the ~> operator mean? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Meaning of ~> in version requirement
I often stumble upon the ~> operator.
eg.
gem 'httparty', '~> 0.5.2'
What does it mean?
It's called a pessimistic version constraint. It matches a gem version by dropping the last digit and comparing equality. For example, ~> 0.5.2 would match version 0.5.2 or 0.5.3, but not 0.5 or 0.6. It's basically equivalent to a constraint of >= 0.5.2, < 0.6.
It means that any version >= 0.5.2 and < 0.6.0
Yehuda Katz recently wrote about this - http://yehudakatz.com/2010/08/21/using-considered-harmful-or-whats-wrong-with/

Resources