2013-08-01 44 views

回答

0

你可以看看Rubinius的配置(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'] 
     ) 

它對我來說同樣好。

+0

這並不能保證你得到可執行文件的實際路徑。剛試過這個自定義ruby安裝,它給了我錯誤的路徑:( – Automatico

0

嘗試%x(where ruby)你必須在你的路徑中有ruby.exe才能工作,所以windows知道你在說什麼'ruby'。

0

以下答案都不可靠。他們使用靜態信息,但是你的ruby腳本可能由安裝在默認路徑以外的ruby實例或不在PATH環境變量中的路徑執行。

你需要做的是使用WIN32 api。特別需要調用GetModuleHandleGetModuleFileName函數。第一個獲取實際進程的句柄,另一個返回它的路徑。

例靈感:

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目錄中讀取。

相關問題