10.2. Enumerable Boolean queries

A number of Enumerable methods return true or false depending on whether one or more element matches certain criteria. Given an array states, containing the names of all the states in the United States of America, here’s how you might perform some of these Boolean queries:

# Does the array include Louisiana?
>> states.include?("Louisiana")
=> true
# Do all states include a space?
>> states.all? {|state| state =~ / / }
=> false

# Does any state include a space? >> states.any? {|state| state =~ / / } => true # Is there one, and only one, state with "West" in its name? >> states.one? {|state| state =~ /West/ } => true # Are there no states with "East" in their names? >> states.none? {|state| state =~ /East/ } => ...

Get The Well-Grounded Rubyist, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.