Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Ruby Blocks!
You have completed Ruby Blocks!
Preview
In this stage, we'll be working towards creating our own class that uses blocks, arrays, hashes, and more. In this video, we'll create the shell class that will hold our monster.
Code Samples
class Monster
attr_reader :name
def initialize(name)
@name = name
end
end
Here's how we can initialize it:
monster = Monster.new("Fluffy")
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
So far, we've learned about blocks
in Ruby and how powerful they are.
0:04
Now we're going to put all of
that knowledge to the test and
0:09
write a program using work spaces.
0:12
We'll be working with Ruby's built in
classes, writing our own classes and
0:14
methods, and practicing with blocks.
0:18
So let's go ahead and get started.
0:21
Okay, so let's go ahead and
create our own class that uses blocks and
0:23
hashes and strings and
everything we've learned so far.
0:28
What we're going to do is make a class for
a monster.
0:32
And a monster is going to be able
to do a few different things.
0:36
And we will do these things using blocks.
0:40
So, let's go ahead and create monster.rb.
0:43
So, first step is to define the class.
0:47
And the class is Monster.
0:53
We're going to go ahead and
0:57
give the monster a name when
we initialize the class.
0:58
And, let's go ahead and
just set that to an instance variable.
1:05
And make an attr_reader out of that so
1:10
we don't have to type the at
sign inside of the class.
1:13
So, first thing that we'll do?
1:20
Let's go ahead and
make the monster say something.
1:23
And the monster will say something, and
then execute whatever is in the block.
1:32
We're going to assume that
when somebody calls this,
1:37
they are going to be printing
something to the screen.
1:42
So we'll just go ahead and
print the name says, so
1:45
that would be the name of the monster,
and the word says.
1:51
So let's go ahead and
create a new monster here.
1:56
And we'll give this monster a very
intimidating monster-like name.
1:59
Fluffy.
2:06
And, the monster will say.
2:07
What's the monster gonna say?
2:13
What's something scary
that a monster would say?
2:15
Welcome to my home.
2:19
Let's go ahead and save that.
2:24
And run it.
2:26
Oh, nothing happened.
2:30
Why?
2:31
Because we didn't call the block or
yield to it.
2:33
So let's go ahead and
yield, and run this again.
2:37
So there we go, Fluffy says...
2:44
Welcome to my home.
2:45
And that's a pretty good start.
2:47
So let's go ahead and start implementing
some more actions in the next video.
2:49
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