Ruby interpreter name [duplicate]
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
开发者_StackOverflow中文版or /usr/bin/ruby myscript.rb
?
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
@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]"
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.
精彩评论