This course will be retired on June 1, 2025.
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
Blocks are easy to use in our classes. In this video, we'll practice writing a class that mimics arrays.
Code Samples
class MyArray
attr_reader :array
def initialize
@array = []
end
def push(item)
array.push(item)
end
def each(&block)
i = 0
while i < array.length
block.call(array[i])
i += 1
end
array
end
end
my_array = MyArray.new
my_array.push(1)
my_array.push(2)
my_array.push(3)
my_array.each{ |item| puts "item: #{item}" }
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
Now that we know how blocks work.
0:00
Let's practice a little bit more by
re-implementing some methods that we've
0:02
already seen.
0:06
Okay.
So the next thing that we're gonna do
0:08
is take a look at a built in
method by re-implementing it.
0:09
And what we're gonna do
0:13
is create a new class that
functions kind of like an array.
0:15
We're gonna call it my_array.rb.
0:19
So this is going to need to be a class,
MyArray.
0:22
So we'll define the class,
0:27
and we'll have the initialize method,
and for
0:32
right now we're going to
use the built in array.
0:36
Let's go ahead make that attr_reader,
0:40
so that we don't have to use the at
sign each time we wanna access it.
0:46
And so we'll make a push method, so
that we can append items to this array.
0:51
We're not doing anything crazy with it.
0:55
But what we really want to do is
see how the each method works,
0:59
so let's implement our own.
1:03
And we can do that by
writing the each method.
1:06
We'll say the each method takes a block.
1:12
So now, what do we do?
1:16
Well, in an array, the each method
iterates over each item in the array,
1:17
and we pass a block to that method.
1:23
So, we could say that the block is
called on each item in the array.
1:27
So, let's go ahead and iterate over each
item, and we'll use a while loop for this.
1:32
We'll say i is zero for our iterator.
1:38
And while i is less
than the array's length,
1:42
we have a block there,
and we return the array.
1:49
So what goes inside this block?
1:53
Well, we'll call the block
that is passed in
1:55
on the current item in the array.
2:02
Now, we could run this right now, but
this is currently an infinite loop.
2:08
What we have to do is increment
our iterator variable.
2:13
So now, let's go ahead and
create a new array.
2:20
And we'll add some items to it.
2:27
And now we can go ahead and
write our block.
2:34
My array.each|item|,
2:38
we'll just say, puts "item".
2:42
Now I'm going to save that and
run this by typing ruby my_array.rb.
2:47
Oh, oh.
2:54
Undefined local variable or
method i for main object.
2:56
And that's on line 14 called from line 26.
3:01
And the problem here is that on line 26,
I tried to call i
3:08
instead of item,
item is what I'm passing in to the block.
3:13
So that would be the issue.
3:20
Let's clear the screen,
and run this again.
3:21
And we can see that this block is
executed for each item on my_array.
3:23
Great job, everybody.
3:29
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