Saturday, February 12, 2011

How can I reliably discover the full path of the Ruby executable?

I want to write a script, to be packaged into a gem, which will modify its parameters and then exec a new ruby process with the modified params. In other words, something similar to a shell script which modifies its params and then does an exec $SHELL $*. In order to do this, I need a robust way of discovering the path of the ruby executable which is executing the current script. I also need to get the full parameters passed to the current process - both the Ruby parameters and the script arguments.

UPDATE: The Rake source code does it like this:

  RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']).
    sub(/.*\s.*/m, '"\&"')

But I'll leave this question open in case anyone has an alternative version.

  • If you want to check on linux: read files:

    • /proc/PID/exe
    • /proc/PID/cmdline

    Other useful info can be found in /proc/PID dir

    From VitalieL
  • for the script parameters, of course, use ARGV :) -r

    From rogerdpack

0 comments:

Post a Comment