We heard earlier in this course that Ruby is an object-oriented programming language. That’s why we’ll dive right into this aspect of the language. The earlier you start working with objects in Ruby, the more you can appreciate the essence of the Ruby programming language itself!
Let’s define what an object is first.
An object in programming mimics a real-world entity like a person, a shopping cart, or even an animal. It’s an entity with different attributes and characteristics that you, as the programmer, can assign or update dynamically depending on how the program executes.
In this course, we’ll be mapping out our own towns and cities in Ruby almost like we’re playing the Sims video game.
If you didn't play this game in the 90s or early 2000s, it's a game in which you can create characters and define their traits, like their names and appearances. You then make them live together in a household and do daily tasks -- which we won't do in this course. 😄
We'll just focus on the process of creating a person in Ruby. The code we write will be much more simple than creating a character in a video game, but you'll get the gist!
We’ll start by creating a class: the
Personclass. We’ll also give each member of the class different attributes in order to create different people with names, ages, and outfits. 👗
What is a class?
A class is a category of something in Ruby. You can think of it like a mold for a particular kind of object.
For example, you could have a mold for building a car. You'd say that the object itself is called a car, and every car should have a brand, a model name, a size, a number of doors, etc.
In Ruby, that'd translate to a class: a
Carclass! You could then create many cars using that initial class.
Classes not only have attributes, but they also have actions available to them. Actions available to the
Carclass would be the ability to drive forward, reverse, park, etc.
As I mentioned, we'll be using the example of a person. Let’s start with defining a class that defines what makes up a person.
A person could have a name, an age, and an outfit, right? I'll translate that to Ruby, and then we'll break it down line by line:
class Person attr_accessor :name, :age, :outfit end
1. To define a class in Ruby, start with the
classkeyword and the name of the class you want to create. Class names in Ruby should be singular and begin with a capital letter. Some examples might be
Product. Don't forget the
classkeyword at the beginning though. 🙂
2. On the next line, we can define what makes up a person. What might a person have on the surface level as attributes? 🤔 As we said, they might have a name, an age, and an outfit.
In Ruby, it's easy to set up these types of attributes on classes! The syntax is deceptively complex though.
attr in Ruby stands for “attribute” (logical enough)! After
attr, you can have a underscore and one of several suffixes, one of which is
attr_accessor , followed by the names of the attributes (each prefaced by a colon), means we can both set and change the attributes’ values, like setting a person’s age and updating it when it changes.
Follow that with an
end to show that you're done defining the class.
Here's the interactive code exercise to help you practice:
If you're working on your computer in Terminal as well (like I do in my videos), go ahead and take all 3 lines of the class definition and paste them into irb. Hit Enter. You don't need to do this if you're using the interactive code exercise above!
Your screen should look like this:
irb(main):001:0> class Person irb(main):002:1> attr_accessor :name, :age, :outfit irb(main):003:1> end => nil
Now you have a Person class! 🎉 You haven't created any real people objects yet because it's just a class definition, kind-of like a mold.
With this mold, you can now create individual people with the attributes you defined above!
We'll see that in the next chapter.