Skip to main content

Quick Tip: Reading Ruby Version from .tool-versions in Your Gemfile

While migrating from asdf to mise as my version manager, I needed to update how my Gemfiles read the Ruby version. It’s a small change, but I thought it might be useful to share.

With asdf, I was using .ruby-version:

ruby File.read(File.expand_path('.ruby-version', __dir__)).chomp

Now with mise using .tool-versions, which can contain versions for multiple languages, the line changed to:

ruby File.read(File.expand_path(".tool-versions"))[/^ruby[@ ](.+)$/, 1]

The regex matches a line starting with “ruby” (followed by either a space or @ symbol) and captures the version number.

That’s it! Just a simple adjustment that keeps your Ruby version in sync with .tool-versions. If you’re using mise, feel free to use this snippet in your Gemfiles.