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 stage, we'll be working towards creating our own class that uses blocks, arrays, hashes, and more. In this video, we'll implement some of the actions our monster can perform.
Code Samples
Here is our Monster class so far:
class Monster
attr_reader :name
def initialize(name)
@name = name
end
def say(&block)
print "#{name} says... "
yield
end
end
And here is how we use it:
monster = Monster.new("Fluffy")
monster.say { puts "Welcome to my home." }
Okay, so we're got our say method,
but let's go ahead and
0:01
have our monster be able
to do a few more things.
0:04
And we're gonna store these things and
count them and
0:07
put a scoreboard on our monster later.
0:12
So, we're just gonna say that
our monster has some actions and
0:15
when we initialize our monster,
0:19
we're gonna keep track of these in
an instance variable called actions.
0:21
And we'll say that our monster,
first off, can scream.
0:27
Isn't that scary?
0:32
And let's go ahead and make the actions
an attribute reader as well.
0:35
So now let's go ahead and define a scream
method, and it's going to take a block.
0:43
And we will yield to the block so
that the block gets called,
0:52
and let's go ahead and
just print to the screen
0:58
name, And screams.
1:03
This method is almost done.
1:10
First thing that we want to do,
is increment
1:13
the number of screams that
our monster has done.
1:18
So now let's go ahead and call this block,
1:25
With our fresh new method, and
1:32
we'll just go ahead and
print BOO to the screen.
1:36
That is pretty scary.
1:42
Now let's go ahead and
run this program, see what happens.
1:45
Uh-oh, undefined method + for NilClass.
1:48
Let's see what's going on here.
1:52
That is on line 25.
1:54
And, on line 17,
1:59
undefined method + for NilClass.
2:02
Let's see what's going on here.
2:08
Oh, we had the wrong key
that we were referencing.
2:10
So now that we fixed that,
let's run it again.
2:19
Okay, Fluffy screams, BOO!
2:23
Now let's go ahead and for
now, just put to the screen
2:26
the action counts here and
see what happens.
2:31
Okay, that makes sense.
2:36
We have one scream, and
that's what we thought.
2:37
And let's just print it
out beforehand too, so
2:43
that we know that it's being incremented.
2:45
But now when we call the scream method,
we know that the block is executed and
2:51
the number of screams in our
internal hash is incremented.
2:56
So, now that we know that that works,
3:02
we can go ahead and
create another couple actions here.
3:04
Let's go ahead and create one for
scares cuz that's what monsters do,
3:08
they scare people.
3:12
And, the scare method's
gonna be the same thing.
3:16
We'll go ahead and
increment the number of scares.
3:24
And then, We'll print
3:29
that the monster has scared you and yield.
3:33
So now we'll call it and
make sure that that works.
3:46
And our monster will scare you by saying,
go away.
3:50
That is pretty scary.
3:54
So I'm going to clear my screen on
the console down here, and run this again.
3:57
Okay.
4:01
That works, and the screams went from
zero to one, and so did the scares.
4:01
And everything is printed out
just like we expect it to.
4:08
Let's make a couple more methods here.
4:15
One for run and one for hide.
4:18
And I'm just gonna paste these in
since they are pretty much exactly
4:22
the same here.
4:27
They are just like the scream and
scare method.
4:30
We also have run and hide, because those
are two things that monsters can do.
4:34
And now all we have to
do is say monster.run.
4:43
Monster is gonna say a scary
thing when it runs and,
4:54
Monster is also gonna say something very
scary when it's running away and hiding.
5:03
Now let's run this real quickly and
see what happens.
5:08
Uh-oh.
5:12
It looks like undefined method + for
NilClass,
5:13
because we forgot to add
these to the actions.
5:18
Okay, let's go ahead and
run this one more time, and
5:34
it looks like that works
just as we expect it to.
5:37
You need to sign up for Treehouse in order to download course files.
Sign up