Rubygems introduced a file option to specify Ruby version in Gemfile
source link: https://blog.saeloun.com/2024/01/17/rubygems-introduced-a-file-option-to-specify-ruby-version/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Rubygems introduced a file option to specify Ruby version in Gemfile
Jan 17, 2024 1 minute readWe 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.
Your Ruby version is 3.1.0, but your Gemfile specified 3.2.2
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
source 'https://rubygems.org'
ruby 3.2.2
.ruby-version
3.2.2
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
source 'https://rubygems.org'
ruby file: '.ruby-version'
.ruby-version
3.2.2
NOTE: Ensure you’re on bundler 2.5.1
or above and rubygems 3.4.20
and above.
# Update rubygems and bundler if required
gem update --system
gem -v
bundle update --bundler
bundle -v
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)
ruby 3.2.2, file: '.ruby-version' # ❌ This will raise an error
ruby 3.2.2 # ✅
ruby file: '.ruby-version' # ✅
- Read the ruby version from the file name provided with
:file
option - normalize the version string
Acceptable version strings are:
3.2.2
ruby-3.2.2
ruby 3.2.2
References
Share this post!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK