如何在Ruby中搜索$PATH中的可执行文件

3

有没有一个 Ruby 函数可以在(Linux)系统路径中搜索可执行文件?

我知道我可以这样做:

path = `which ls` # found if $?.exitstatus == 0

但是有没有一种 Ruby 的方式呢?类似这样:

find_in_path('ls') # => '/bin/ls'

阅读答案 - 我理解这个没有内置的方法? - constantine1
1个回答

3

find_executable方法。

>> require 'mkmf'
=> true
>> find_executable 'ls'
checking for ls... yes
=> "/bin/ls"
>> find_executable 'no-such-command'
checking for no-such-command... no
=> nil

不知道为什么这个没有文档...无论如何还是谢谢。也许最好手动搜索 ENV['PATH']。 - constantine1
未记录,因为它仅用于内部使用 - NARKOZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接