In Python, you can do
在Python中,您可以这样做
>>> import sys
>>> sys.executable
'/usr/bin/python'
to get at the executable's location. Can you do the same thing just using something built-in to Ruby? It can be a special variable, method, etc.
获取可执行文件的位置。你能用Ruby内置的东西做同样的事情吗?它可以是一个特殊的变量、方法等。
If there isn't, what is the cleanest, most reliable way of determining the ruby executable's location in a cross-platform way?
如果没有,用跨平台的方式确定ruby可执行文件的位置的最干净、最可靠的方法是什么?
Related:
相关:
- How to get the python.exe location programmatically?
- 如何获得python。exe位置以编程方式?
5 个解决方案
#1
6
Run this in IRB:
在IRB运行这个:
require 'rbconfig'
key_length = RbConfig::CONFIG.keys.max{ |a,b| a.length <=> b.length }.length
RbConfig::CONFIG.keys.sort_by{ |a| a.downcase }.each { |k| puts "%*s => %s" % [key_length, k, RbConfig::CONFIG[k]] }
It will output an "awesome print" style list of all the Ruby configuration info.
它将输出所有Ruby配置信息的“很棒的打印”样式列表。
ALLOCA =>
AR => ar
arch => x86_64-darwin10.5.0
ARCH_FLAG =>
archdir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.5.0
ARCHFILE =>
AS => as
ASFLAGS =>
BASERUBY => ruby
bindir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin
bindir
is the path to the currently running Ruby interpreter. Above it in the list is BASERUBY => ruby
.
bindir是当前运行的Ruby解释器的路径。在列表的上面是BASERUBY => ruby。
RbConfig::CONFIG.values_at('bindir', 'BASERUBY').join('/')
=> "/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby"
Checking my work:
检查我的工作,
greg-mbp-wireless:~ greg$ which ruby
/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
There's a lot more information than this so it's worth running the code I added above to see what's available.
这里有更多的信息,所以运行我上面添加的代码看看有什么可用的。
#2
2
Linux-based systems are OK with
基于linux的系统可以接受
`whereis ruby`.split(" ")[1]
It will call whereis ruby
and parse its' output for the second entry (first contains 'whereis:')
它将调用whereis ruby并解析它的“第二个条目的输出”(首先包含“whereis:”)
The more strict method is to call
更严格的方法是调用。
puts `ls -al /proc/#{$$}/exe`.split(" ")[-1]
It will get the executable name for the current process (there is $$ variable and Process.pid method to obtain that) from /proc/pid/exe symlink information.
它将获得当前进程的可执行名称(有$$变量和进程。pid方法获取)从/proc/pid/exe符号链接信息。
#3
1
Works in a script, not from irb
:
在剧本中工作,不是来自irb:
puts open($PROGRAM_NAME).readline.gsub /#! *([^ ]+).*/, '\1'
;-)
:-)
#4
1
It looks like the answer is in RbConfig::CONFIG I think RbConfig::CONFIG['bindir'] provides the directory where the executable is located, the rest is s (or should be) straight forward.
看起来答案在RbConfig::CONFIG中,我想是RbConfig: CONFIG['bindir']提供了可执行文件所在的目录,其余的是s(或应该是)。
RbConfig::CONFIG['bindir']+'/ruby' should work, even in windows as the exe can be ommitted
配置['bindir']+'/ruby'应该可以工作,即使是在windows中,因为exe可以省略
#5
1
Looks like the only truly reliable way is
看起来唯一真正可靠的方法是
system("#{Gem.ruby} another_file.rb")
This works even for odd cases like jruby being run as a jar, etc.
这甚至适用于像jruby作为jar运行的情况,等等。
Also see
也看到
OS.ruby_bin
https://github.com/rdp/os
#1
6
Run this in IRB:
在IRB运行这个:
require 'rbconfig'
key_length = RbConfig::CONFIG.keys.max{ |a,b| a.length <=> b.length }.length
RbConfig::CONFIG.keys.sort_by{ |a| a.downcase }.each { |k| puts "%*s => %s" % [key_length, k, RbConfig::CONFIG[k]] }
It will output an "awesome print" style list of all the Ruby configuration info.
它将输出所有Ruby配置信息的“很棒的打印”样式列表。
ALLOCA =>
AR => ar
arch => x86_64-darwin10.5.0
ARCH_FLAG =>
archdir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.5.0
ARCHFILE =>
AS => as
ASFLAGS =>
BASERUBY => ruby
bindir => /Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin
bindir
is the path to the currently running Ruby interpreter. Above it in the list is BASERUBY => ruby
.
bindir是当前运行的Ruby解释器的路径。在列表的上面是BASERUBY => ruby。
RbConfig::CONFIG.values_at('bindir', 'BASERUBY').join('/')
=> "/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby"
Checking my work:
检查我的工作,
greg-mbp-wireless:~ greg$ which ruby
/Users/greg/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
There's a lot more information than this so it's worth running the code I added above to see what's available.
这里有更多的信息,所以运行我上面添加的代码看看有什么可用的。
#2
2
Linux-based systems are OK with
基于linux的系统可以接受
`whereis ruby`.split(" ")[1]
It will call whereis ruby
and parse its' output for the second entry (first contains 'whereis:')
它将调用whereis ruby并解析它的“第二个条目的输出”(首先包含“whereis:”)
The more strict method is to call
更严格的方法是调用。
puts `ls -al /proc/#{$$}/exe`.split(" ")[-1]
It will get the executable name for the current process (there is $$ variable and Process.pid method to obtain that) from /proc/pid/exe symlink information.
它将获得当前进程的可执行名称(有$$变量和进程。pid方法获取)从/proc/pid/exe符号链接信息。
#3
1
Works in a script, not from irb
:
在剧本中工作,不是来自irb:
puts open($PROGRAM_NAME).readline.gsub /#! *([^ ]+).*/, '\1'
;-)
:-)
#4
1
It looks like the answer is in RbConfig::CONFIG I think RbConfig::CONFIG['bindir'] provides the directory where the executable is located, the rest is s (or should be) straight forward.
看起来答案在RbConfig::CONFIG中,我想是RbConfig: CONFIG['bindir']提供了可执行文件所在的目录,其余的是s(或应该是)。
RbConfig::CONFIG['bindir']+'/ruby' should work, even in windows as the exe can be ommitted
配置['bindir']+'/ruby'应该可以工作,即使是在windows中,因为exe可以省略
#5
1
Looks like the only truly reliable way is
看起来唯一真正可靠的方法是
system("#{Gem.ruby} another_file.rb")
This works even for odd cases like jruby being run as a jar, etc.
这甚至适用于像jruby作为jar运行的情况,等等。
Also see
也看到
OS.ruby_bin
https://github.com/rdp/os