tags:

views:

85

answers:

2

Inside a ruby script, how do I get the path to the ruby interpreter?

Example script:

  #!/path/to/ruby
  puts `#{RUBY_INTERPRETER_PATH} -e "puts 'hi'"`
  #EOF

Where RUBY_INTERPRETER_PATH is a mythical way of finding /path/to/ruby.

This is just an example, though. I realize in this case that I could just copy /path/to/ruby into the script, but I don't want to do that. I want this to work "correctly" regardless of what the #! line says. Even if running under windows.

Ciao!

+7  A: 

To get the path of the currently running ruby interpreter:

require 'rbconfig'
RUBY_INTERPRETER_PATH = File.join(Config::CONFIG["bindir"],
                                  Config::CONFIG["RUBY_INSTALL_NAME"] +
                                  Config::CONFIG["EXEEXT"])
mckeed
source: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151376
ryeguy
A: 

I have seen this construct before:

#!/usr/bin/env ruby
puts "Hi"

On Windows, you need add the "rb" extension to PATHEXT.

SET PATHEXT=.RB;%PATHEXT%

And make sure .rb files are associated to your ruby interpreter.

CodexDraco