问题
I have a Rails 3 application which has the following line in the Gemfile.
gem 'sqlite3', '1.3.6'
However, if I list my local gems I get:
> gem list sqlite3 --local
*** LOCAL GEMS ***
sqlite3 (1.3.6, 1.3.4)
When my Rails apps does a
require 'sqlite3'
which version of the gem is selected? Is it that the first gem in the list is selected? Is there a way to tell the Ruby runtime to use version 1.3.4 even if version 1.3.6 is installed, and mandated by the Gemfile?
回答1:
You could find out with
bundle exec gem list sqlite3
回答2:
Either the Gemfile
will specify a version or Gemfile.lock
will have the version.
Examples:
Gemfile:
gem 'tiny_tds', '0.5.0'
Gemfile.lock:
tiny_tds (0.5.0)
Edit: if you want to see the version, use iltempos' suggestion or in the rails console
type
1.9.3p194 :001 > SQLite3::VERSION
=> "1.3.6"
回答3:
the Gemfile
list all the dependencies of your Rails application, you can add constrains about the version to use for each gem. In your example you specified that your application depends on sqlite3 1.3.6 so it will use the version 1.3.6.
In general the exact version of the gems required by your application are in the Gemfile.lock
.
There are several syntaxes you can you to specify the versions:
gem 'gemname', '1.2.3'
- Requires gemname version 1.2.3gem 'gemname', '>= 1.2.3'
- Requires gemname version 1.2.3 or any higher version. Can break thingsgem 'gemname', '~> 1.2'
- Require gemname 1.2 or a minor update like 1.2.9. You get updates but not mayor one which could break compatibility
The interesting thing to note is that once the Gemfile.lock
is written and checked in your version control, it's shared between all the members of the team that all will use the same, exact, versions of the required gems.
If you need to update a required gem you can do that in 2 steps:
- Update the
Gemfile
if needed - Run
bundle update gemname
The step 2 will download the new gem version (if there is a new version respecting the constrain in the Gemfile
), install it and update the Gemfile.lock
accordingly.
来源:https://stackoverflow.com/questions/14141206/ruby-rails-which-version-of-a-gem-is-used