docwhat
docwhat

Reputation: 11704

How do I find the ruby interpreter?

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!

Upvotes: 18

Views: 12995

Answers (2)

rogerdpack
rogerdpack

Reputation: 66961

These days (1.9+) you can use built-in methods (which are supposed to work with Jruby, etc.) like this:

RbConfig.ruby or Gem.ruby

$ irb --simple-prompt
>> RbConfig.ruby
=> "C:/installs/Ruby193/bin/ruby.exe"
>> Gem.ruby
=> "C:/installs/Ruby193/bin/ruby.exe"

Upvotes: 20

mckeed
mckeed

Reputation: 9827

To get the path of the currently running ruby interpreter:

require 'rbconfig'
RUBY_INTERPRETER_PATH = File.join(RbConfig::CONFIG["bindir"],
                                  RbConfig::CONFIG["RUBY_INSTALL_NAME"] +
                                  RbConfig::CONFIG["EXEEXT"])

Upvotes: 15

Related Questions