We add ruby version in the
Gemfile or in the
This ensures that the application runs on the correct version of Ruby.
In case of version mismatch bundler throws an error.
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
rbenv to switch the ruby version.
The problem with this approach is that we have to update the version in both the files.
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
Gemfile will automatically read the 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
:fileoption is provided
- Check if both
:fileoption 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
- normalize the version string
Acceptable version strings are: