RVM shebang in a ruby script
mouse 1023 · person cloud · link
Last update
2024-09-30
2024
09-30
« — »

run script with the default ruby version

  • option A:
1
2
#!/usr/bin/env -S ${HOME}/.rvm/bin/rvm-shebang-ruby
puts RUBY_VERSION
  • option B:
1
2
#!/usr/bin/env -S ${HOME}/.rvm/bin/rvm default do ruby
puts RUBY_VERSION

run script with desired ruby version

1
2
#!/usr/bin/env -S ${HOME}/.rvm/bin/rvm ruby-3.0.0 do ruby
puts RUBY_VERSION

fix relative path script calls

1
2
3
./apps/myscript.rb   # works
~/apps/myscript.rb   # works
apps/myscript.rb     # hangs!

edit ~/.rvm/bin/rvm-shebang-ruby and modify the last case with *rvm-shebang-ruby:

1
2
3
4
5
#rvm_promptless=1 rvm rvmrc load "$(dirname "$(which "$1")")" > /dev/null 2>&1
folder="$(dirname "$(which "$1")")"
[ "$folder" = "." ] && folder=$PWD
folder="$(realpath -s "$folder")"
rvm_promptless=1 rvm rvmrc load "$folder" > /dev/null 2>&1