More enumerables

Learn more about predicate enumerable methods.

Introduction

In the previous lesson, you learned about some really handy enumerable methods like each, map, and select. In this lesson, we're going to learn about even more enumerable methods! Woohoo!

This time, we're focusing on a particular subset of enumerable methods: the predicate enumerable methods. You should recall from the Methods lesson that a predicate method is indicated by a question mark (?) at the end of the method name and returns either true or false. Again, we won't be going through all of the predicate enumerable methods, so be sure to have a look at the Ruby docs to see what else Enumerable offers.

For all of the examples throughout this lesson, feel free to follow along in irb or repl.it (an online REPL environment) to get a better feel for how they work.

Learning outcomes

By the end of this lesson, you should be able to do the following:

  • Describe what a predicate enumerable method is.

  • Explain how the #include? method works.

  • Explain how the #any? method works.

  • Explain how the #all? method works.

  • Explain how the #none? method works.

The include? method

The #include? method works exactly like you think it should. If we want to know whether a particular element exists in an array, we can use the #include? method. This method will return true if the element you pass as an argument to #include? exists in the array or hash; otherwise, it will return false.

First, let's explore how we would achieve this with the #each method:

numbers = [5, 6, 7, 8]
element = 6
result = false

numbers.each do |number|
  if number == element
    result = true
  end
end

result
# => true

element = 3
result = false

numbers.each do |number|
  if number == element
    result = true
  end
end

result
#=> false

Using #include?, this code can be greatly simplified:

numbers = [5, 6, 7, 8]

numbers.include?(6)
#=> true

numbers.include?(3)
#=> false

For another example, let's return to the friends and invited_friends arrays from the previous lesson:

friends = ['Sharon', 'Leo', 'Leila', 'Brian', 'Arun']

invited_list = friends.select { |friend| friend != 'Brian' }

invited_list.include?('Brian')
#=> false

The any? method

You might also be able to guess what the #any? method does. It returns true if any elements in your array or hash match the condition within the block; otherwise, it will return false.

Let's say we want to see if there is any number greater than 500 or less than 20 in an array of numbers. First, let's see how we could achieve this using #each.

numbers = [21, 42, 303, 499, 550, 811]
result = false

numbers.each do |number|
  if number > 500
    result = true
  end
end

result
#=> true

numbers = [21, 42, 303, 499, 550, 811]
result = false

numbers.each do |number|
  if number < 20
    result = true
  end
end

result
#=> false

Using #any?, this code can be greatly simplified:

numbers = [21, 42, 303, 499, 550, 811]

numbers.any? { |number| number > 500 }
#=> true

numbers.any? { |number| number < 20 }
#=> false

The all? method

The all? method is also fairly intuitive. It only returns true if all the elements in your array or hash match the condition you set within the block; otherwise, it will return false.

Let's say that we want to check whether all the words in our list are more than 6 characters long. First,let's see how we could achieve this using #each:

fruits = ["apple", "banana", "strawberry", "pineapple"]
matches = []
result = false

fruits.each do |fruit|
  if fruit.length > 3
    matches.push(fruit)
  end

  result = fruits.length == matches.length
end

result
#=> true

fruits = ["apple", "banana", "strawberry", "pineapple"]
matches = []
result = false

fruits.each do |fruit|
  if fruit.length > 6
    matches.push(fruit)
  end

  result = fruits.length == matches.length
end

result
#=> false

Using #all?, this code can be greatly simplified:

fruits = ["apple", "banana", "strawberry", "pineapple"]

fruits.all? { |fruit| fruit.length > 3 }
#=> true

fruits.all? { |fruit| fruit.length > 6 }
#=> false

Special note to keep in mind while debugging: #all? will return true by default unless the block returns false or nil. So if you call #all? on an empty array or hash (i.e., there are no elements for the block to evaluate), it will return true.

The none? method

As you might expect, #none? performs the opposite function of #all?. It returns true only if the condition in the block matches none of the elements in your array or hash; otherwise, it returns false.

First, let's see how this could be achieved using #each. You'll notice that this approach is very similar to what we did for #all?.

fruits = ["apple", "banana", "strawberry", "pineapple"]
matches = []
result = false

fruits.each do |fruit|
  if fruit.length > 10
    matches.push(fruit)
  end

  result = matches.length == 0
end

result
#=> true

fruits = ["apple", "banana", "strawberry", "pineapple"]
matches = []
result = false

fruits.each do |fruit|
  if fruit.length > 6
    matches.push(fruit)
  end

  result = matches.length == 0
end

result
#=> false

Using #none?, this can be greatly simplified:

fruits = ["apple", "banana", "strawberry", "pineapple"]

fruits.none? { |fruit| fruit.length > 10 }
#=> true

fruits.none? { |fruit| fruit.length > 6 }
#=> false

Assignment

  1. Read How to Use Ruby Any, All, None, and One for alternative explanations for predicate enumerables.

  2. Complete the predicate enumerable exercises from the ruby-exercises repo that you previously cloned.

Additional resources

This section contains helpful links to other content. It isn't required, so consider it supplemental for if you need to dive deeper into something.

  • There are many more enumerable methods than are covered in this lesson (e.g., #member? and #one?). For a full listing, you can check out the Ruby Docs.

Knowledge check

This section contains questions for you to check your understanding of this lesson. If you're having trouble answering the questions below on your own, review the material above to find the answer.

  • Why is there a question mark after some method names?

  • What does the include? method do?

  • What does the any? method do?

  • What does the all? method do?

  • What does the none? method do?

Last updated