We add ruby version in the Gemfile
or in the .ruby-version
files.
This ensures that the application runs on the correct version of Ruby.
In case of version mismatch bundler throws an error.
Before
We needed to add the ruby version in the Gemfile
so the bundler uses correct ruby version or warns if incorrect ruby version is in use.
.ruby-version
file is used by tools like rvm
or rbenv
to switch the ruby version.
Gemfile
.ruby-version
The problem with this approach is that we have to update the version in both the files.
After
Rubygems introduced a :file option that can read the Ruby version from a file.
Now its easier as we have to just update the version in the .ruby-version
file and
the Gemfile
will automatically read the version.
Gemfile
.ruby-version
NOTE: Ensure you’re on bundler 2.5.1
or above and rubygems 3.4.20
and above.
How this works?
- The method checks if
:file
option is provided - Check if both
:file
option as well as the ruby version is provided and raise an error (Only one is required)
- Read the ruby version from the file name provided with
:file
option - normalize the version string
Acceptable version strings are:
References