Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this workshop, we'll learn all about the Ruby Enumerable module and see different examples of how it works.
Code Samples
array = [1, 2, 3, 4, 5, 6]
array.each { |item| puts "Item: #{item}" }
array.each_with_index {|item, index| puts "[#{index}] #{item}"}
array.member?(3)
array.all? { |item| item > 3 }
array.any? { |item| item > 3 }
array.detect {|item| item > 3 }
array.select {|item| item > 3 }
# array.find_all is the same
array.map { |item| item + 3 }
puts array.take(2)
hash = {name: "Jason", location: "Treehouse", position: "Teacher"}
puts hash.map{|k,v| "#{k}: #{v}"}.inspect
puts hash.take(2).inspect
puts array.inject{|sum, item| sum += item}
puts array.inject(&:+)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Hi.
0:04
I'm Jason, your Ruby teacher here,
at Treehouse.
0:05
In this workshop, we're going to be
going over the enumerable module.
0:08
The enumerable module is a mix-in that
can be used in our Ruby classes that
0:13
lets us get a lot of behavior for free.
0:17
We're gonna go ahead and see how a lot of
those methods work now in this workshop.
0:20
So let's go ahead and get started.
0:24
Okay so now we're going to take
a look at the Enumerable module,
0:28
and we're gonna take a look at it
in a little bit more depth and
0:32
view some demonstrations about how to use
some of these methods in our classes here.
0:36
So let me go ahead and launch irb.
0:44
So I'm gonna create some
variables to work with here.
0:45
First one is array.
0:50
It's gonna have some elements in it.
0:52
And a hash.
0:57
Okay.
So we've got this array and
1:07
this hash right here.
1:09
So the first method we're gonna
take a look at is the each method.
1:11
So, with an array, which includes the
enumerable module, each looks like this.
1:14
Array.each {|item|, and
we'll just print it out.
1:19
And then it returns the array, and
all of the items are printed out.
1:27
Now, when we use the each method with
a hash, we get a key and a value.
1:33
And we could print it out that way.
1:38
So, we should be used to seeing these.
1:47
Any class that implements or
1:50
includes the enumerable module
needs to provide an each method.
1:52
And then, once the each method
is provided, we get some really
1:57
interesting methods that we can
do with it for example, member.
2:02
So we can say array.member,
and pass in an item.
2:06
Yes, it does have the number 3.
2:11
It does not have the number 30.
2:14
Now with a hash.
2:17
We can ask if the key is included.
2:21
So for example the name key is included,
but names are not.
2:26
And then we get really
cool methods like all.
2:30
So let's take a look at
our array right here.
2:35
And we can say array.all, and
then we get this block to pass into it.
2:36
And then we could pass in
some sort of statement.
2:43
Is the item greater than 3?
2:46
And no, it's not.
2:49
But we could also ask the array if any
of the items are greater than three.
2:51
And we can do the same thing with hashes.
2:59
Now these are methods we've
probably seen before.
3:09
There's also detect.
3:12
So let's go ahead and
look at the array again.
3:14
And if we call the detect method,
pass of the block would be item
3:21
being greater than 3,
it returns the number four.
3:25
Now if we go and
look back at the documentation for detect.
3:29
We can see that is says it passes
each entry to the block, and
3:34
returns the first for
which the block is not false.
3:38
So in our case it returned four
because that would be the first
3:43
item which met this test which
is that it is greater than 3.
3:48
And that's pretty much
the same as the find method.
3:52
Which returns four.
3:59
So detect and find are the same.
4:00
But, there's also array.find_all.
4:03
Which would return 4, 5 and 6, which
is the same thing as the select method.
4:10
So those are just duplicate methods.
4:19
And that works with arrays, hashes and
anything that implements enumerable.
4:20
But let's get to the interesting methods.
4:25
For example the map method.
4:28
So here's our array.
4:31
It's got these six different items.
4:32
And let's go ahead and say we wanted
to multiply all of these by 2.
4:35
Well, what we could do
is say array.map item.
4:40
item * 2.
4:47
And now we get the multiple
of each item in the array.
4:50
And then for
4:54
some reason let's say we wanted to
convert all of these into strings.
4:55
Well we can do that to, in fact,
if we did our math, we could map it again.
5:01
So we get back an array of strings.
5:10
Something to keep in mind when
working with the map method
5:14
is that it will always return an array.
5:19
So, if we had our hash, for
example, we would say hash.map key,
5:21
value, and then,
we could just map the values.
5:27
And, that would give us back
an array of just the value.
5:30
So, it should say, Jason,
Teacher, Treehouse.
5:32
And then,
we could do the same thing with the keys.
5:35
The important thing to note is that
the map method is always going to return
5:38
an array in this instance.
5:42
So, if we went back up,
we could still map it again.
5:45
And, transform this result to
the number of characters in the word.
5:48
So that would be 5, 7, and 9 characters.
5:56
Now this winds up being very,
very powerful because you can very,
6:00
very quickly transform your different
arrays, hashes, or any enumerable class.
6:03
So here's another really
interesting method.
6:12
We've got our array, and there's usually
a lot of confusion about this method.
6:14
And the method is inject.
6:18
Let's go ahead and
take a look at the inject documentation.
6:20
So if we scroll down here,
6:26
it says it combines all elements of
enum by applying a binary operation,
6:28
specified by a block or
symbol that names a method or operator.
6:34
And then if you specify a block,
each element is passed to an accumulator.
6:40
What in the world does that mean?
6:44
Well, it's actually pretty easy.
6:46
And we can think about doing it as a sum.
6:49
So we've got an array of
integers right here, and
6:52
let's say we wanted to add them all up.
6:56
We could do array.inject, and
6:59
we would say sum, element, and then sum
7:04
will be equal to the addition of element.
7:10
Now when I press Enter you can
see that this returns 21 which
7:15
is the sum of every item in this array.
7:20
So what happens is, this particular
element that goes in as an argument to
7:24
the block is, kept between iterations,
and then passed to
7:29
the next iteration of the block with
the same value that it had before.
7:34
And we could do the same thing with minus,
and that returns -19.
7:39
And, we can also, if we really wanted to,
specify the beginning value.
7:44
So, that beginning value would
be passed to the accumulator,
7:51
which is the first argument.
7:55
So if we did it for zero,
it would be the same thing.
7:57
We could also start it at say 10.
8:04
All right.
8:12
Whoop misspelled it there.
8:13
And that gives us 31 which
is 10 more then we started.
8:22
So the inject method can be very, very
powerful and save us just a lot of typing.
8:28
I mean this is all something
that we could write ourselves.
8:33
Maybe in a method.
8:36
If we really wanted to get it,
we would say that the sum zero
8:37
array.each item sum += item.
8:41
Now if we look at sum,
it is 21 just like it was before.
8:47
But if we use the inject method,
we could pass the result of each item in
8:50
the block right back to that accumulator
variable without writing our own method,
8:55
or even creating that
temporary sum variable.
9:01
And there are a ton of different
methods in enumerable.
9:04
We went over just a few of them.
9:07
Feel free to go through
the documentation and
9:09
try all of the different methods just
to see which ones you like the best, or
9:13
which ones you think might be valuable
when coding your Ruby programs.
9:17
It's very important to be familiar
with the enumerable module,
9:22
because a lot of ruby classes and
libraries will implement it.
9:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up