Sunday, 11 August 2013

Processing Array and returning boolean value

Processing Array and returning boolean value

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

No comments:

Post a Comment