• 20 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 2/6/20

Create a person with a name, age, and outfit

Log in or subscribe for free to enjoy all this course has to offer!

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

  • create variables

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).

 https://www.codevolve.com/api/v1/publishable_key/2A9CAA3419124E3E8C3F5AFCE5306292?content_id=90b8e896-4bc2-44c1-a37d-0a2467e58572

Create a new person

The first and most logical-seeming thing to do would be to create a new person using thePersonclass 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:

Person.new

However, if you simply typePerson.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:

=> #<Person:0x007fb6d7929768>

Yikes! That's not a very fun way to reference the person we just created. 😕

Variables

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 asperson_2,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. 

Setting attributes

Within yourPersonclass, 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 settingperson_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 aPerson"object" or an "instance of thePersonclass" withname,age, andoutfitattributes.

Try doing the same using a person you know. Enter the commandperson_2 = Person.new, hit enter, and set their attributes one-by-one exactly as we did above.

Example of certificate of achievement
Example of certificate of achievement