What does the ~> operator mean? [duplicate] - ruby

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/

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.

"Could not find activesupport" when 3 versions installed

I'm just trying to use the simple career_builder gem and just get it imported by running the simple script:
require 'career_builder'
puts 'Hello world!'
and yet get the following error: /Library/Ruby/Site/2.0.0/rubygems/dependency.rb:315:in 'to_specs': Could not find 'activesupport' (~> 2.3.5) - did find: [activesupport-4.2.1,activesupport-3.1.12,activesupport-3.0.3] (Gem::LoadError)
I installed the gem with gem install career_builder and ran bundle install and even updated activesupport to the most recent version, but for some reason, the program can't find the newer version of activesupport. Does the gem require version 2.3.5?
http://guides.rubygems.org/patterns/
The ~>or 'twiddle-waka' is a ruby gems shortcut to specify the earliest version of the gem you can use without letting you go up to the next major release.
Your gem is being a bit unorthodox and also specifying a patch level.
So the gem_specification you're working with (activesupport' (~> 2.3.5)) really means minimum version of 2.3.5 maximum of the last patch released before 2.4.0.
The activesupport versions you have installed are all for subsequent major releases and won't work. Install something between 2.3.5 and 2.4.0 and you should be good to go.
Yes. It does require Active Support version >= 2.3.5 and < 2.4.0. All of your Active Support versions are > 2.4.0.
~> is called the spermy operator. See Meaning of tilde-greater-than (~>) in version requirement?
The gem has not been updated in 4 years, so it uses Rails 2.
FWIW, I don't think you'll have much luck getting it to work, so you may want to find a similar gem that works with Rails 4 and has been updated within the last few months.

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.

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

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.

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

Resources