次のような他のスクリプトには変数があります。
パイソン:print(sys.executable)
php:echo PHP_BINARY."\n";
ルビーにそのようなものはありますか?
(ルビー v.2.0)
以下の答えはどれも確実に機能しません。これらは静的情報を使用しますが、Ruby スクリプトは、デフォルト パスとは異なるパスまたは PATH 環境変数にないパスにインストールされた Ruby インスタンスによって実行される可能性があります。
必要なことは、WIN32 API を使用することです。特に、GetModuleHandleおよびGetModuleFileName関数を呼び出す必要があります。最初のものは実際のプロセスのハンドルを取得し、もう 1 つはそのパスを返します。
インスピレーションの例:
require 'ffi'
module Helpers
extend FFI::Library
ffi_lib :kernel32
typedef :uintptr_t, :hmodule
typedef :ulong, :dword
attach_function :GetModuleHandle, :GetModuleHandleA, [:string], :hmodule
attach_function :GetModuleFileName, :GetModuleFileNameA, [:hmodule, :pointer, :dword], :dword
def self.actualRubyExecutable
processHandle = GetModuleHandle nil
# There is a potential issue if the ruby executable path is
# longer than 999 chars.
rubyPath = FFI::MemoryPointer.new 1000
rubyPathSize = GetModuleFileName processHandle, rubyPath, 999
rubyPath.read_string rubyPathSize
end
end
puts Helpers.actualRubyExecutable
/proc
Linux では、この情報はディレクトリから読み取ることができます。
rubinius configure( build_ruby )を見てください。
したがって、現在は次のようになっています。
require 'rbconfig'
def build_ruby
unless @build_ruby
bin = RbConfig::CONFIG["RUBY_INSTALL_NAME"] || RbConfig::CONFIG["ruby_install_name"]
bin += (RbConfig::CONFIG['EXEEXT'] || RbConfig::CONFIG['exeext'] || '')
@build_ruby = File.join(RbConfig::CONFIG['bindir'], bin)
end
@build_ruby
end
私も次のことを試しました:
require 'rbconfig'
File.join( RbConfig::CONFIG['bindir'],
RbConfig::CONFIG['RUBY_INSTALL_NAME'] + RbConfig::CONFIG['EXEEXT']
)
それは私にも同じように機能します。
これが機能するには、パスに ruby .exe%x(where ruby)
が必要です。これにより、Windows は、あなたが話している「ルビー」を認識することができます。