2

In Python, you can do

>>> import sys
>>> sys.executable
'/usr/bin/python'

to get at the executable's location. Can you do the same thing just using something built-in to Ruby? It can be a special variable, method, etc.

If there isn't, what is the cleanest, most reliable way of determining the ruby executable's location in a cross-platform way?

Related:

Community
  • 1
  • 1
ento
  • 5,801
  • 6
  • 51
  • 69

5 Answers5

5

Run this in IRB:

require 'rbconfig'

key_length = RbConfig::CONFIG.keys.max{ |a,b| a.length <=> b.length }.length
RbConfig::CONFIG.keys.sort_by{ |a| a.downcase }.each { |k| puts "%*s => %s" % [key_length, k, RbConfig::CONFIG[k]] }

It will output an "awesome print" style list of all the Ruby configuration info.

   ALLOCA => 
       AR => ar
     arch => x86_64-darwin10.5.0
ARCH_FLAG => 
  archdir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.5.0
 ARCHFILE => 
       AS => as
  ASFLAGS => 
 BASERUBY => ruby
   bindir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin

bindir is the path to the currently running Ruby interpreter. Above it in the list is BASERUBY => ruby.

RbConfig::CONFIG.values_at('bindir', 'BASERUBY').join('/')
=> "/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby"

Checking my work:

greg-mbp-wireless:~ greg$ which ruby
/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby

There's a lot more information than this so it's worth running the code I added above to see what's available.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
2

Looks like the only truly reliable way is

 system("#{Gem.ruby} another_file.rb")

This works even for odd cases like jruby being run as a jar, etc.

Also see

 OS.ruby_bin

https://github.com/rdp/os

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
2

Linux-based systems are OK with

`whereis ruby`.split(" ")[1]

It will call whereis ruby and parse its' output for the second entry (first contains 'whereis:')

The more strict method is to call

puts `ls -al /proc/#{$$}/exe`.split(" ")[-1]

It will get the executable name for the current process (there is $$ variable and Process.pid method to obtain that) from /proc/pid/exe symlink information.

kirushik
  • 1,296
  • 1
  • 10
  • 20
  • +1 to /proc/pid/exe. However I guess we have to craft a special cross-platform glue method to answer this question. I hope someone (matz?) would come out and say it right out that "There's no such thing built-in to ruby". – ento Oct 13 '09 at 09:40
1

It looks like the answer is in RbConfig::CONFIG I think RbConfig::CONFIG['bindir'] provides the directory where the executable is located, the rest is s (or should be) straight forward.

RbConfig::CONFIG['bindir']+'/ruby' should work, even in windows as the exe can be ommitted

0

Works in a script, not from irb:

puts open($PROGRAM_NAME).readline.gsub /#! *([^ ]+).*/, '\1'

;-)

andre-r
  • 2,685
  • 19
  • 23
  • I know, also env isn't the interpreter. It is not supposed to be very serious anyway. – andre-r Oct 08 '09 at 10:58
  • I got that from the smiley ;-) Is there really no internal variable that tells us the path of the executable being run? I had a quick look at my Ruby cookbook but couldn't find anything. – Arthur Reutenauer Oct 08 '09 at 11:04