Regex 新手正则表达式问题确定是否在操作系统中可执行
我在匹配此输出的“是”字符串部分时遇到问题(如果不存在可执行文件,我会假设它说“否”,但可能有更好的跨平台解决方案?) 这是我写的代码:Regex 新手正则表达式问题确定是否在操作系统中可执行,regex,ruby,Regex,Ruby,我在匹配此输出的“是”字符串部分时遇到问题(如果不存在可执行文件,我会假设它说“否”,但可能有更好的跨平台解决方案?) 这是我写的代码: require 'mkmf' logs = find_executable 'docker-compose' if logs.match(/yes/) puts "hi" else puts "bye" end 它不停地说“再见” 我做错了什么?您有几个问题: logs变量不包含您认为它的功能正在检查dock
require 'mkmf'
logs = find_executable 'docker-compose'
if logs.match(/yes/)
puts "hi"
else
puts "bye"
end
它不停地说“再见”
我做错了什么?您有几个问题:
require 'mkmf'
p find_executable('docker-compose') ? 'found' : 'not found'
#=> "found"
p find_executable('no-such-file') ? 'found' : 'not found'
#=> "not found"
如果愿意,您当然可以使用if-then或case语句,但三元运算符提供了一种更简洁的方式来表达原始帖子的语义
require 'mkmf'
p find_executable('docker-compose') ? 'found' : 'not found'
#=> "found"
p find_executable('no-such-file') ? 'found' : 'not found'
#=> "not found"