Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取在windows上运行当前脚本的ruby.exe的路径?_Ruby_Windows - Fatal编程技术网

如何获取在windows上运行当前脚本的ruby.exe的路径?

如何获取在windows上运行当前脚本的ruby.exe的路径?,ruby,windows,Ruby,Windows,其他脚本中也有变量,如: python:print(sys.executable) php:echo php\u二进制文件。“\n” ruby中有类似的东西吗 (ruby v.2.0)您可以看看rubinius configure() 因此,目前情况如下: require 'rbconfig' def build_ruby unless @build_ruby bin = RbConfig::CONFIG["RUBY_INSTALL_NAME"] || RbConfig::CONF

其他脚本中也有变量,如:

python:
print(sys.executable)

php:
echo php\u二进制文件。“\n”

ruby中有类似的东西吗


(ruby v.2.0)

您可以看看rubinius configure()

因此,目前情况如下:

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']
         )

它对我来说同样有效。

试试
%x(其中ruby)
你必须在你的路径中有ruby.exe,这样windows才知道你说的是什么“ruby”。

下面的答案都不可靠。它们使用静态信息,但ruby脚本可能由安装在不同于默认路径或不在path环境变量中的路径中的ruby实例执行

您需要做的是使用WIN32 api。特别是,需要调用和函数。第一个获取实际进程的句柄,另一个返回其路径

灵感的例子:

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安装上尝试了这个,但它给出了错误的路径:(