I'm wondering, what would be the easiest way to check if all the elements of Array conform to certain criteria and return boolean? Is there maybe a pattern in Ruby to call method on collection and then return boolean value? Standard Enumerable methods return either Array or nil, so I'm not sure where to look.I've wrote an example that works using grep, but I feel that if could be skipped with more idiomatic code:
def all_matched_by_regex?(regex)
array_collection = ['test', 'test12', '12test']
matched = array_collection.grep(regex)
if matched.length == array_collection.length
return true
end
return false
end
all?behaviour with empty collections (it returns true in such cases). I'd suggest checking any? as well, just reversing the pattern to check against.