2

Possible Duplicate:
How do I find the ruby interpreter?

How do I get the currently running Ruby 1.8 interpreter name in Ruby (e.g. /usr/bin/ruby), i.e. the argv[0] passed to the C main() function. I'm not interested in $0, because that's the name of the .rb script file. I'm also not interested in Config::CONFIG, because that was filled when Ruby was installed -- but I'm interested in where it is running now.

Let's suppose /usr/bin/ruby is a symlink to /usr/bin/ruby1.8. How do I get to know if my Ruby script has been started as /usr/bin/ruby1.8 myscript.rb or /usr/bin/ruby myscript.rb?

Community
  • 1
  • 1
pts
  • 80,836
  • 20
  • 110
  • 183

3 Answers3

1

See How do I find the ruby interpreter?

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

If you want Ruby specific information check out the RUBY_* constants

>> RUBY_
RUBY_COPYRIGHT     RUBY_ENGINE        RUBY_PLATFORM      RUBY_REVISION
RUBY_DESCRIPTION   RUBY_PATCHLEVEL    RUBY_RELEASE_DATE  RUBY_VERSION
Community
  • 1
  • 1
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
  • Thank you for trying to help, but this is not a good answer to my question, because I am not interested in where the interpreter was copied when it was installed (that's what I can get from `CONFIG::Config`), but I'm interested in where the interpreter currently is (i.e. the `argv[0]` of the C `main` function). What I'm interested in is not in `Config::CONFIG`. – pts Dec 05 '10 at 17:28
0

@injekt's answer has the path to the interpreter.

Here's how to find the particulars about the configuration.

Ruby's configuration info is stored in rbconfig.rb during compilation so we can see the particulars of the installation. That information is pulled into Object when the interpreter starts so we can get at the values:

>> Object.constants.select{ |c| c[/^RUBY/] }
=> [:RUBY_VERSION, :RUBY_RELEASE_DATE, :RUBY_PLATFORM, :RUBY_PATCHLEVEL, :RUBY_REVISION, :RUBY_DESCRIPTION, :RUBY_COPYRIGHT, :RUBY_ENGINE]

>> RUBY_DESCRIPTION #=> "ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0]"
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Thank you for providing interesting information about Ruby, but what you wrote doesn't answer my question. – pts Dec 05 '10 at 17:30
  • As I said, "@injekt's answer has the path to the interpreter." I am expanding on it showing other information that is useful. – the Tin Man Dec 06 '10 at 00:15
0

Here is a Linux-only solution:

p File.open("/proc/self/cmdline") { |f| f.read.sub(/\0.*/m, "") }

For Ruby 1.8, ruby.c defines VALUE rb_argv0; which contains this information, but that variable is not available in Ruby scripts.

pts
  • 80,836
  • 20
  • 110
  • 183