其他脚本中有变量,例如:
python:print(sys.executable)
的PHP:echo PHP_BINARY."\n";
ruby 中有这样的东西吗?
( ruby v.2.0)
最佳答案
以下所有答案均无法可靠工作。它们使用静态信息,但是您的ruby脚本可能由安装在默认路径以外的路径或不在PATH环境变量中的路径中的ruby实例执行。
您需要做的是使用WIN32 API。特别是,需要调用GetModuleHandle和GetModuleFileName函数。第一个获取实际过程的句柄,另一个返回其路径。
灵感示例:
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
在Linux上,可以从
/proc
目录中读取此信息。
关于ruby - 如何获取在Windows上运行当前脚本的ruby.exe的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18005358/