简化“任何"内容在Ruby中与or-operator进行检查 [英] Simplifying an "any of" check with or-operator in Ruby

查看:76
本文介绍了简化“任何"内容在Ruby中与or-operator进行检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何简化以下检查?...

How to simplify the following check ?...

if node[:base][:database][:adapter].empty? || node[:base][:database][:host].empty? || 
  node[:base][:database][:database].empty? || node[:base][:database][:port].empty? 

类似于

required_keys = { :adapter, :host, :database...etc...} 
required keys - node[:base][:database] == [] 

此语法有点差,但基本上是从一组必需的键中减去您拥有的键.如果您的集合中有所有必需的键,则结果应该为空.

This syntax is a little off, but basically subtract the keys you have from the set of required keys. If you have all the required keys in your set, the result should be empty.

我不确定语法正确吗? .任何帮助将不胜感激

I am not sure regarding the correct syntax ? . Any help would be appreciated

推荐答案

required_keys = [:adapter, :host, :database ]
if required_keys.any?{|x| node[:base][:database][x].empty?}
  #code here
end

或者您也可以:

node[:base][:database].values_at(*required_keys).any?(&:empty?) 

这篇关于简化“任何"内容在Ruby中与or-operator进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆