Skip to content

Instantly share code, notes, and snippets.

@EdwardDiehl
Last active February 26, 2022 11:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdwardDiehl/9c660b59d0fb2e936774ee2c4fcfc792 to your computer and use it in GitHub Desktop.
Save EdwardDiehl/9c660b59d0fb2e936774ee2c4fcfc792 to your computer and use it in GitHub Desktop.
Read and specify ruby version in Gemfile from asdf-vm .tool-versions file.
# https://gist.github.com/EdwardDiehl/9c660b59d0fb2e936774ee2c4fcfc792
# Read and specify ruby version in Gemfile from asdf-vm .tool-versions file.
# Copy the file to your your project root.
# Add these lines at the top of your project Gemfile:
# require './ruby_version.rb'
# ruby ruby_version
def ruby_version(ruby_default_version = '2.5.0')
tool_versions_file = File.join(File.expand_path('..', __FILE__), '.tool-versions')
if File.readable?(tool_versions_file)
ruby_version = IO.foreach(tool_versions_file, "\n") do |tool_version|
tool = Hash[*tool_version.split(' ')]
break tool['ruby'] if tool['ruby']
end
if ruby_version
puts "Set ruby version from #{tool_versions_file} file: #{ruby_version}"
return ruby_version
end
puts "No ruby version found in #{tool_versions_file} file, set ruby version to default: #{ruby_default_version}"
else
puts "No #{tool_versions_file} file found, set ruby version to default: #{ruby_default_version}"
end
ruby_default_version
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment