Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creating a Class3:56 with Jason Seifer
In this video, we create a simple class. The class will be modeled after the concept of a name and return the different attributes a name might have.
class Name def title "Mr." end def first_name "Jason" end def middle_name "" end def last_name "Seifer" end end name = Name.new puts name.title puts name.first_name puts name.middle_name puts name.last_name
Now that we know a little bit about how objects and 0:00 classes work, lets create our own class. 0:02 We'll start small and create a class for representing a name. 0:06 Before we jump straight in to coding, lets think about how a name can be structured. 0:11 We're not going to get into everything that can be possible with a name. 0:16 But for the purposes of the class we're going to write, a name could have 0:20 the following items, title, first name, middle name, and last name. 0:24 We create a class using the class keyword, and 0:31 then the name of the class that we wanna create. 0:34 Let's see how that works now using Work Spaces. 0:37 So let's go head and create our first class. 0:41 I've launched a Ruby Workspace here, and on the left, 0:45 I'm going to right click and choose new file. 0:48 And we'll this name.rb. 0:53 And this is gonna be a simple class that just returns my name. 0:56 So let's go ahead and dig in. 1:03 We start writing a class by using the keyword class. 1:06 Then we type the name of the class. 1:10 Using an uppercase letter to start with. 1:14 In this case, the class name is going to be name. 1:18 [SOUND] And then we close it, by writing end. 1:21 Now, if we wanted to have a longer class name, that was more specific. 1:29 The convention behind that is doing something like, 1:35 say, FirstName with no spaces. 1:38 But we're just gonna keep the name class here, and let's go ahead and 1:43 think about the different components of a name. 1:48 First we'll start with the title and 1:51 we're gonna create methods for all of these different attributes. 1:53 And we'll say title and I'll go with mister this is a string. 1:58 And I have a first name, that's Jason. 2:08 [NOISE] I don't have a middle name but 2:15 I'm going to return an empty string and 2:19 I do have a last name, and that's going to return Seifer. 2:24 So now that we have this class written, let's go ahead and instantiate it. 2:32 And let's just print out the name. 2:38 Go ahead and click into the console and type ruby name.rb. 2:46 Now this returned, well, pretty much nothing it has the name of the class, and 2:52 a colon, and a bunch of weird symbols here. 2:56 And this is actually the address in memory of the instantiated object. 3:01 But let's go ahead and 3:10 actually do something with the data that we have inside here. 3:11 So we'll print out the title. 3:15 We're just calling a method. 3:18 [SOUND] And now we are calling all of 3:23 the different methods of this name object 3:29 which is an instance of the name class. 3:36 Now let's run this again and see what happens. 3:43 And it did exactly what we expected. 3:48 And print it out. 3:50 The results of these different methods. 3:52
You need to sign up for Treehouse in order to download course files.Sign up