Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';从Ruby检查系统上是否存在命令行工具的最优雅的方法是什么?_Ruby - Fatal编程技术网

什么';从Ruby检查系统上是否存在命令行工具的最优雅的方法是什么?

什么';从Ruby检查系统上是否存在命令行工具的最优雅的方法是什么?,ruby,Ruby,为了便于参考,这里讨论了Bash等价物: 我一直在做类似的事情 if `which commandname` =~ /commandname/ # do stuff end 但是我想知道是否有更干净的东西。也许if File.executable?(`which commandname`)只要调用该命令,并捕获在该命令不存在时引发的异常。 system("ls") #=> true system("ls wrong params") #=> false system("ls

为了便于参考,这里讨论了Bash等价物:

我一直在做类似的事情

if `which commandname` =~ /commandname/
  # do stuff
end

但是我想知道是否有更干净的东西。

也许
if File.executable?(`which commandname`)
只要调用该命令,并捕获在该命令不存在时引发的异常。
system("ls")
#=> true

system("ls wrong params")
#=> false

system("lss")
#=> nil

if system("your cmd")
  puts "yey!"
else
  puts "oups"
end