如何在Ruby中搜索$ PATH中的可执行文件[重复]

问题描述 投票:3回答:1

这个问题在这里已有答案:

是否有一个ruby函数来搜索(Linux)系统路径中的可执行文件?

我知道我可以这样做:

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

但它有一种红宝石的方式吗?就像是:

find_in_path('ls') # => '/bin/ls'
ruby
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
© www.soinside.com 2019 - 2024. All rights reserved.