Is there a way to tell RVM to use the latest Ruby that is at least 2.0.0?

I often do a lot of scripts in Ruby, and sometimes I run those scripts as assigned by Jenkins, or put them where others can run it locally.

I would like to specify in .rvmrc something like:

  • Use the most recent version of rubies that is installed
  • If it is less than Ruby 2, then it crashes.

This way I can depend on Ruby 2 language changes (like named parameters), but without forcing the environment using the script to install the new Ruby if it already has 2.0.0 or 2.1.1 or 2.1.4 installed.

+3


source to share


1 answer


You should be able to run bash commands on the file .rvmrc

. So you can check the latest version and require it and the default is 2.0.0 if not found. I'm not sure what you mean for the crash as this file is loaded when starting a terminal session and aborting it would be bad.

Here's an example I created using rbenv

sine. I have not rvm

.

RV=`rbenv versions | grep -E " 2\.\d+\.\d+\S*" | grep -o -E "2\.\d+\.\d+\S*" | sort | tail -n 1`
if [[ $RV ]]
  then echo $RV
  else echo "DEFAULT"
fi

      



This example just prints the highest version 2.xx of ruby, which finds it differently than DEFAULT. For RVM .rvmrc

, the following may work in your file , although I cannot test it myself. I based it on the findings found in the docs. You may need to make some adjustments.

RV=`rbenv versions | grep -E "ruby-2\.\d+\.\d+\S*" | grep -o -E "2\.\d+\.\d+\S*" | sort | tail -n 1`
if [[ $RV ]]
  then rvm use $RV
  else rvm use 2.0.0
fi

      

0


source







All Articles