Locked lesson.
About this lesson
More on classes and how to use them.
Exercise files
Download this lesson’s related exercise files.
Classes Part 2.docx60.4 KB Classes Part 2 - Solution.docx
59.5 KB
Quick reference
Classes Part 2
Let's look at classes in more depth.
When to use
Classes are an essential part of object oriented programming.
Instructions
To initialize a class (start it up!) we create a new object:
my_square = Square.new(10)
Be sure to pass your parameter in!
To inspect our new object:
puts my_square.inspect
Hints & tips
- my_square = Square.new(10)
- puts my_square.inspect
- 00:04 In this video, we're going to continue on with classes.
- 00:07 In the last video we created a class, and we created an initialize method.
- 00:11 And now, how do we do something with this?
- 00:14 How do we use this?
- 00:15 Well, to start out you need to create an object to initialize this thing.
- 00:20 So let's create it, let's call it my_square.
- 00:24 Call it whatever you want, doesn't really matter.
- 00:26 And =.
- 00:27 Now, to kind of fire this thing up and make it work,
- 00:30 we type in the name of the class, Square, and then .new.
- 00:34 That's sort of how you initialize it.
- 00:36 Now, you would maybe think you would type in .initialize, but no, for
- 00:40 some reason it's .new
- 00:42 And then, be sure to pass your parameter.
- 00:44 So let's say we want a side_length of 5.
- 00:47 So now if we save this, come up here, and run our program, nothing happens,
- 00:52 because all we've really done here is kind of start our class.
- 00:55 We fired it up, but it doesn't really do anything yet.
- 00:57 In fact, we can come up here and we can go puts my_square,
- 01:02 because now our square is this guy right here,
- 01:05 this my_square is an object of the Square class.
- 01:09 It is a square, and we can puts it to the screen.
- 01:12 But if we do, you're going to see it's kind of weird here.
- 01:15 We get this gobbledygook.
- 01:17 All this is, this is an address in memory where our square resides.
- 01:23 Doesn't mean anything to us at all.
- 01:24 We can go a little bit further and type in inspect,
- 01:28 which will let us inspect our square.
- 01:31 And you see, now it says the same weird gobbledygook, but
- 01:35 it also has our instance variable, our side_length=5, because we passed in 5.
- 01:41 If we changed this to 10 and ran it, now it's going to say side_length=10.
- 01:47 Every time we sort of initialize our class and
- 01:51 pass it stuff, a new instance of our Square class runs.
- 01:55 And you'll notice, so this is an address in memory where our square resides.
- 02:00 Every time we run this, you'll see we get a new address because it's creating
- 02:04 a whole new square every time we run it.
- 02:05 So a lot of these things I'm talking about are going to be a little abstract and
- 02:10 weird, you might not understand exactly what's going on, and
- 02:13 we're just going to motor through this and start using these things.
- 02:16 And as we do, you'll start to see, okay, this starts to make more sense.
- 02:20 Right now I just want to show you the format of a class, how you start them.
- 02:25 You initialize every class you ever create,
- 02:27 you're going to initialize like this, and pass in some sort of parameter, and
- 02:31 then set that parameter equal to an instance variable.
- 02:34 And we haven't really talked about instance variables,
- 02:36 I'll talk about them more in the next video.
- 02:39 But you know I mentioned earlier the idea of a t-shirt factory, and
- 02:42 a class is a blueprint?
- 02:44 I can come to it and I can say, I want a blue t-shirt, I want a black t-shirt.
- 02:48 Well, same thing here.
- 02:50 We've got a class with a square and we're saying,
- 02:52 I want a square with sides of 10, or, I want a square with sides of 20.
- 02:57 All of this stuff stays the same, but we can pass in our whatever we want into it,
- 03:03 to make our demands to get what we want.
- 03:05 So that's sort of an abstract concept, and
- 03:07 it'll become more clear, I promise, as we go on.
- 03:09 I know this is kind of weird at the beginning, but classes, super important.
- 03:13 In the next video we're going to talk more about instance variables.
- 03:16 So that's all for this video.
Lesson notes are only available for subscribers.