I have to write a Ruby method that:
Iterates through an array, doing Foo if one of the elements matches a certain condition.
If none of the array elements matched the condition, do Bar thing.
In any other language, I'd set a Boolean variable before entering the loop and toggle it if I did Foo. The value of that variable would tell me whether I needed to Bar. But that feels unRubyishly inelegant. Can anybody suggest a better way?
Edit Some really good answers, but they don't quite work because of a detail I should have mentioned. The something that Foo does is done to the array element that matches the condition. Also, it's guaranteed that at most one element will match the condition.
Do any of the items match? If yes, then do something, not involving the matching item.
if items.any? { |item| item.totally_awesome? }
foo "we're totally awesome!"
else
bar "not awesome :("
end
Grab the first matching item. If it exists, then do something, with the matching item.
awesome_item = items.find { |item| item.totally_awesome? }
if awesome_item
foo "#{awesome_item.name} is totally awesome!"
else
bar "no items are awesome :("
end
Grab all matching items. If the array has anything in it, then do something with all matching items.
awesome_items = items.find_all { |item| item.totally_awesome? }
if awesome_items.any?
foo "#{awesome_items.size} totally awesome items!"
else
bar "no items are awesome :("
end
You could do it like this:
if array.any? { |elem| elem.condition }
foo
else
bar
end
From the doc, Enumerable#any
does the following:
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With