Locked lesson.
About this lesson
What is a class and how do we use them?
Exercise files
Download this lesson’s related exercise files.
Classes.docx60.3 KB Classes - Solution.docx
57.8 KB
Quick reference
Classes
Let's learn about classes!
When to use
Classes are used to group a bunch of methods together.
Instructions
To create a class called Square:
class Square
def initialize(side_length)
@side_length = side_length
end
end
Hints & tips
- Class names are uppercase
- The first method of a class is initialize
- 00:04 In this video, I want to start talking about classes.
- 00:07 And classes are a pretty advanced topic in any programming language, but
- 00:10 they're not that bad.
- 00:11 A class is an object-oriented thing, and
- 00:14 Ruby is an object-oriented programming language, so classes, it's the thing.
- 00:19 Remember in the last video we talked about methods?
- 00:21 And methods are ways to kind of keep together chunks of code that we want to
- 00:25 use at different times.
- 00:26 Well, a class is a way to group a bunch of methods together.
- 00:31 It's sort of like a blueprint, it's its own little program, sort of, and
- 00:35 objects are defined from a class.
- 00:37 I said in the last video, we talked about random numbers,
- 00:40 that this Random was a method.
- 00:41 That wasn't completely true.
- 00:42 Random is a class, this .rand is a method of the Random class.
- 00:48 So methods come from classes, right?
- 00:51 A thing you gotta have to wrap your brain around.
- 00:53 And a class, it's sort of like a blueprint, right?
- 00:55 Like if you have a T-shirt factory, they have a blueprint for creating a T-shirt,
- 01:00 and that T-shirt might be blue, it might be red, but it's a T-shirt.
- 01:03 There's a plan for it.
- 01:05 And you can have different methods that create different types of T-shirts,
- 01:08 like a blue T-shirt, a red T-shirt, a large T-shirt, a small T-shirt, but
- 01:12 the overall blueprint is the same for the whole thing.
- 01:15 It's stamping out T-shirts, right?
- 01:17 So you can come to your factory and say, give me a blue one, and
- 01:20 it knows what to do.
- 01:21 It goes to the class,
- 01:22 it says we're going to create a T-shirt with a method of blue, right?
- 01:26 So that's sort of a way to think about classes.
- 01:28 They're very abstract, and you really just kind of have to dive and
- 01:31 start building them in order to kind of understand them.
- 01:34 But they're very important for more advanced programming things.
- 01:38 So not all programming languages have classes, they're relatively new.
- 01:42 They're an object-oriented type thing, and
- 01:44 object-oriented programming is relatively new.
- 01:47 Ruby takes advantage of it, obviously.
- 01:48 So let's go ahead and get rid of some of this stuff.
- 01:55 So to create a class, remember way back at the beginning
- 01:58 of the course we couldn't create a variable with an uppercase name or
- 02:01 with an uppercase letter because that's reserved for classes?
- 02:04 So to define a class you just type in the word class, it's a keyword, and
- 02:09 then name it, and let's call it Square class.
- 02:12 And you'll notice it's uppercase.
- 02:14 And you'll also notice it's kind of yellow,
- 02:15 that means it's definitely different than normal.
- 02:17 And we can end it.
- 02:19 So far it's the same.
- 02:20 So inside of your class you're going to have methods, lots of methods, or
- 02:24 a few methods, however many you want.
- 02:26 But a class is really a collection of other methods, and
- 02:29 then you can call those methods from the outside world.
- 02:32 But the first method in any class is called initialize.
- 02:36 So let's create a method and let's call it initialize, there we go.
- 02:41 If your code doesn't work, chances are you misspelled initialize.
- 02:44 And like any method we can pass a parameter, so
- 02:48 what would be the main essence of a square?
- 02:51 It'd be probably its side length, because a square has four equal sides.
- 02:55 So let's create a parameter and let's call it side_length, and we can end it.
- 03:03 So we're good on initialize now, now this thing will work.
- 03:06 It doesn't do anything at this point, but we have now created a class.
- 03:11 If we save this and run our program, again, nothing happens.
- 03:15 Because this code doesn't get executed like a method unless you call it later on,
- 03:19 and we haven't called it yet.
- 03:20 So this is the basic look and feel of a class, this is how it's composed.
- 03:25 So now to pass this parameter in, we need to assign it to a variable.
- 03:29 So we could go side_length = side_length.
- 03:34 And you normally want to create these variables of the same
- 03:37 name that you passed in as a parameter.
- 03:39 But there's a little bit difference, you put an @ in front of this.
- 03:42 And now this becomes what's called an instance variable, and
- 03:45 we'll talk more about that later on.
- 03:47 But now our Square class is created, it's initialized,
- 03:50 doesn't really do much of anything yet.
- 03:53 But in the next video, we'll dive in and
- 03:54 start doing some more cool stuff with this.
- 03:56 So that's all for this video.
Lesson notes are only available for subscribers.