The none? method takes an enumerable collection and tests whether the given condition is true for exactly NONE of the items in the collection.
- View the objects used in these examples
- View these examples as a runnable ruby script on GitHub.
- Practice this method as an interactive kata on Codewars
Number List
@numbers = [1, 0, 3, 2, 5, 4, 7, 6, 9, 8] # no even numbers? @numbers.none?(&:even?) #=> false # no number 15? @numbers.none?{|number| number == 15} #=> true
This is the exact opposite of the all? method.
Pet Inventory
# no pets sold out? @inventory.none?(&:sold_out?) #=> false # no pets in stock? @inventory.none?(&:in_stock?) #=> false # no pets with three legs? @inventory.none?{|pet| pet.legs == 3} #=> true # no pets with four legs? @inventory.none?{|pet| pet.legs == 4} #=> false
Pokey Things
# no pokey things with the word cactus? @pokey_things.seek(0) @pokey_things.none?{|thing| thing =~ /cactus/} #=> false # no pokey things that start with the letter z? @pokey_things.seek(0) @pokey_things.none?{|thing| thing[0] == 'z'} #=> true
Heroku Log File
# no errors? @requests.none?(&:error?) #=> false # no 404 errors? @requests.none?{|request| request.status == 404} #=> true