Reputation: 9407
I know that an empty string would be consider truthy, since only nil and false are considered falsey. But I have an array of empty strings like this:
["", "", ""].any?
=> true
And I want it to return false if all the strings in the array are empty. One option is to do this:
["", "", ""].select {|item| item.present? }.any?
=> false
But now I am using two iterators in this example. Is this the only way? Or is there another iterator in the arsenal of tools that suites the job?
Upvotes: 2
Views: 3104
Reputation: 42099
any
should take a block, so you should be able to do something like:
["", "", ""].any? {|item| item.present?}
This can be shortened using a to_proc
method on the present?
symbol (credit: @mu is too short, 2018):
['','',''].any?(&:present?)
Upvotes: 5
Reputation: 8513
In my head the logic would be like, "they're not all empty" so I would put
arr = ["","",""]
!arr.all?(&:empty?)
=> false
Using .present?
on a string doesn't work in my testing environment.
Upvotes: 2