I have a hash, i want to know if any param does not exist or is empty
how is does not exist? in this case does not exist the param 'b'
{"a"=>"first", "c"=>"5"}
and empty element is like this: "a"=>""
{"a"=>"", "b"=>"b", "c"=>"5"}
this is my attempt:
array.any?{|_,i| p i.blank?}
output:
if there is (all or any empty elements) then return true
[true, false]
output true
[true, false,true]
output true
[true, true]
output true
[true, true, true]
output true
[false, false]
output false
[false, false, false]
output false
CodePudding user response:
h = { "a" => "", "c" => "5" }
keys = h.keys
missed_keys = [*keys.first..keys.last] - keys
puts 'Missed Keys'
p missed_keys
puts 'Keys which are having empty values'
p h.filter_map { |k, v| k if v.empty? }
Output
Missed Keys
["b"]
Keys which are having empty values
["a"]
update: To only return true or false.
p h.any? { |k, v| v.empty? }
output
true
CodePudding user response:
I think Ruby has blank?
function to do this
hash = {"a": 1, "c": ""}
def is_missing(hash, key)
hash[key.to_sym].blank?
end
is_missing(hash, "a") => false
is_missing(hash, "b") => true
is_missing(hash, "c") => true