I have a script in a Ruby application at bin/setup
that begins with:
#!/usr/bin/env ruby
env
tells what executables are accessible, and if there is a Ruby version available on $PATH
, the script should use it to execute.
Suppose I am using rbenv
and I have a global Ruby version set (either system
which falls back to /usr/bin/ruby
on OS X) or something else. system
or my global Ruby version is different from the .ruby-version
declared in the direction within which this bin/setup
script is being executed. The .ruby-version
file contains 2.1.6
. This causes the script to instantly abort with:
rbenv: version `2.1.6' is not installed (set by /Users/olivierlacan/project/.ruby-version)
This means there is no way to rely on env
to dynamically fetch the appropriate version of Ruby this bin/setup
script should be executed with.
The only alternative I found is:
#!/usr/bin/ruby
But this is not portable to any Linux or Windows OS that doesn't have Ruby installed in /usr/bin/
like OS X. Is there a way around this?