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:
Using #include?
, this code can be greatly simplified:
For another example, let's return to the friends
and invited_friends
arrays from the previous lesson:
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
.
Using #any?
, this code can be greatly simplified:
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
:
Using #all?
, this code can be greatly simplified:
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?
.
Using #none?
, this can be greatly simplified:
Assignment
Read How to Use Ruby Any, All, None, and One for alternative explanations for predicate enumerables.
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