Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplifying an "any of" check with or-operator in Ruby

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? 

to something like

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

like image 887
Bearish_Boring_dude Avatar asked Dec 27 '22 03:12

Bearish_Boring_dude


1 Answers

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

Or you could do also:

node[:base][:database].values_at(*required_keys).any?(&:empty?) 
like image 191
Arup Rakshit Avatar answered Jan 14 '23 16:01

Arup Rakshit