You've created a sort of "mold" for people by creating a
Person class. Now it's time to create some people!
In Ruby, just creating a class isn't enough. A class gives you a definition of something (like what a person is, what a shopping cart is, what a product is, etc), but it doesn't automatically create objects of that class.
In this chapter, you'll see how to:
create a person
set the attributes of the person
This may seem like a lot, but these operations often go together in Ruby.
Check out this interactive code exercise that will help you follow along with the chapter. As usual, you can also follow along in the Terminal application on your computer if you prefer (which I do in my screencast).
Create a new person
The first and most logical-seeming thing to do would be to create a new person using the
Personclass we defined, right?
In order to create a new "instance" of a class, you call the name of the class plus the keyword "new" which ends up looking like:
However, if you simply type
Person.new(which you should feel free to do either in irb or in the interactive code exercise above) and type Enter, your computer will return this nasty output:
Yikes! That's not a very fun way to reference the person we just created. 😕
Code syntax can be tough and overly complex sometimes. Your computer can read that giant line above with all its letters and numbers, but it's not practical for humans. That's why, if we can give more human-readable names to things in our code, we should. Variables are a great way to do this.
For example, when you create your person object, you can assign its value (that nasty 0x007fb6d line above) to something more clear, like
person_1. We can then reference subsequent people we create as
person_3, etc. Much more readable!
Here's what your new line of code will look like:
person_1 = Person.new
Type it into your interactive code exercise (or in Terminal if you're following along that way) and press Enter. You'll still see that a gnarly string like the one above gets created, but you can now use
person_1to reference it instead.
Personclass, you defined that a person should have a name, an age, and an outfit. To set attributes of an object in Ruby, you'll reference the object and use dot notation to set an attribute equal to a value.
To set a person's name, you'd type:
person_1.name = "Arnaud"
From left to right in this line, you're literally setting
person_1's name equal to a set of text, which is "Arnaud." Once you type this line of code in irb, you can press Enterto submit it.
Can you think of how you'd set his age then? It's similar! Again, reference the variable (
person_1), type a period, the attribute name, and set it equal to a value (remembering to hit Enter again to submit the command).
person_1.age = 32
Lastly, you can set the person's outfit attribute:
person_1.outfit = ["blue top", "red pants", "white shoes"]
At this point, you've created your first person!
In human-speak: you've created a person who has a name, age, and outfit!
In Ruby-speak: you've created a
Person"object" or an "instance of the
Try doing the same using a person you know. Enter the command
person_2 = Person.new, hit enter, and set their attributes one-by-one exactly as we did above.