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.
Related
I like to setup the version of Ruby I am using on each project by setting up a .ruby-version file, but I find it very strict, especially if I am sharing my code.
If I declare this for example:
#.ruby-version
3.0.2
The code is going to request this ruby version and it won't accept any other no even: 3.0.4 which I know it will also work. This makes my code less sharable.
Is there any way I can use version description syntax like in the Gemfile file?
>= 3.0.0
>= 3.0.2, < 3.3
~> 3.0
It's funny that you even said "like in the Gemfile...", because you can define the required ruby version in the Gemfile, instead of having a .ruby-version file:
ruby '~> 3.0' # or whatever
gem 'some-dependency'
gem 'another-dependency'
# ...
There is similar question here.
Ruby: Rails: Which version of a gem is used?
But it's about when Gemfile is used.
I want to know if there is no Gemfile, which version of gem is used.
For example I have 4 versions of selenium-webdriver in my system.
% gem list | grep selenium
selenium-webdriver (2.53.0, 2.48.1, 2.46.2, 2.45.0)
And I just use it by pry and require 'selenium-webdriver. How can I know which version is used? Only the latest is selected?
You probably have more than one gem because each project with Gemfile in your machine have one different version of it.
When using the gem via require or command line without specifying the version the last one - the greater version - will be automatically used.
By convention, in the most part of the cases, you can print the version doing the following:
require 'some_gem'
puts SomeGem::VERSION
# => "3.0.3"
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.
Here's the relevant part of my gemspec:
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 2.13.0"
spec.add_dependency "addressable"
spec.add_dependency "activesupport", "> 3.0.11"
Travis is failing for ruby 1.8.7 because it tries to install ActiveSupport 4.0, but Rails 4 does not support ruby 1.8.7. I'm afraid this might be an issue when users try to use the gem, even though it might be compatible with 1.8.7.
How can I fix this, while also keeping Rails 4 support? I don't want to use "~> 3.0.11" on my gemspec.
I would suggest altering the contents of your gemspec to something like:
if RUBY_VERSION < "1.9"
spec.add_dependency "activesupport", "~> 3.0.11"
else
spec.add_dependency "activesupport", "> 3.0.11"
end
Technically this does use the syntax you don't want, but it does so in a constrained way, and should not adversely impact users. In fact, it only affects the build process of the gem, and will make Travis use a restricted version of the dependency (as an end user would have to anyway) without placing any constraints on the gem in general - provided you package and release the gem whilst using a more recent Ruby.
I'd suggest using the appraisal gem for this. It allows you to generate multiple Gemfiles for the project and then you can specify the gemfiles like this:
language: ruby
rvm:
- 1.9.3
- 2.0.0
gemfiles:
- gemfile/3.0.gemfile
- gemfile/4.0.gemfile
matrix:
include:
- rvm: 1.8.7
gemfile: gemfile/3.0.gemfile
This will generate five builds:
1.9.3 + 3.0
2.0.0 + 3.0
1.9.3 + 4.0
2.0.0 + 4.0
1.8.7 + 3.0
Hope something like this helps.
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