2

次のような他のスクリプトには変数があります。

パイソン:print(sys.executable)

php:echo PHP_BINARY."\n";

ルビーにそのようなものはありますか?

(ルビー v.2.0)

4

3 に答える 3

1

以下の答えはどれも確実に機能しません。これらは静的情報を使用しますが、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

/procLinux では、この情報はディレクトリから読み取ることができます。

于 2014-09-19T08:24:14.213 に答える
0

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

それは私にも同じように機能します。

于 2013-08-01T22:24:05.770 に答える
0

これが機能するには、パスに ruby ​​.exe%x(where ruby)が必要です。これにより、Windows は、あなたが話している「ルビー」を認識することができます。

于 2014-08-26T11:31:46.167 に答える