Today you’ll learn about 4 Enumerable methods that will help you check a conditional statement against an array of elements, a hash, or any other objects that include the Enumerable module.
I will stick to size == 1 because it’s more explicit. Everyone will understand that even if they aren’t familiar with the one? method, which is not that common.
New Ruby 2.5 Feature
Since Ruby 2.5 these 4 methods (any? / all? / none? / one?) also take an argument which works like grep’s argument.
Just to pile on: a lot of people think that .any? checks if an enumerable contains any elements, and .none? checks if there are no elements (like .empty?). There’s a nasty gotcha lurking in that interpretation.
These don’t check whether elements exist, but whether they make the block return a truthy value (or if you don’t pass a block, then whether they are truthy). So frex [nil, false].any? is false, and [nil, false].none? is true.