Now that you have some basic understanding of classes, it’s time to dive a little deeper into understanding object orientation.
In the last lesson, we observed the following code:
Look at lines 13, 14 and 15, above, where we assign a value to the first_name property of each object. This is the first time we actually do something with “objects of type Person”. In lines 17,18 and 19, we are sending the value stored in the first_name property to the terminal. In all six lines of code, it’s we who are doing something with them (the objects). The objects themselves aren’t doing anything…. yet.
When we write object-oriented code, we use objects created from classes to model the world around us.. or at least.. model the things we need to model for the purpose of our application. Think of yourself as a real world object. In computer terms, you have two categories of capabilities: state and behavior. To be clear, when we say behavior in this sense, we mean the ability to do things.
OK.. let’s address state first. Surely you’ve heard the term “state of mind”, yes? Sometimes your state of mind is happiness.. other times it is sadness. Worry, anxiety, anger.. you get the picture. We also have other “kinds” of state: our body temperature, blood pressure, cholesterol.. any number of things. Very often, the only way to get at that state is to make some sort of inquiry, or test.
Behavior, on the other hand, is the name we give to our ability to perform actions… at least in computer programming parlance. For example, we walk, talk, breathe, eat, read, etc. Notice that some of our behaviors don’t require any additional input, such as walking, or talking, however, others require some sort of additional matter or input. For example, we cannot eat without food, nor can we read without words in front of us. In computer programming terms, these additional things which are required for us to perform some action are called parameters or arguments (there is a subtle difference between the two terms, but for our beginner’s purposes, we’ll think of them as completely synonymous now and differentiate them once you have these concepts more solidly formed in your mind).
Forgetting about Ruby specifically… objects (models of real world things), deal with state and behavior by storing values in properties (for state), and encapsulating behavior inside of methods.
For example.. it makes sense that a human resources application needs to model people. However, it doesn’t need to model human beings in all of our mind-blowing complexity. We only need to model those attributes of a person that are required for the application to do its intended work.
Let us now suppose that we want to give our Person class a new capability… the ability to introduce itself. . Before we can do that though, we need to revisit our class definition. The line that reads “attr_accessor :first_name” is Ruby’s rather kludgy way of declaring a property. It is what makes lines 13-15 above possible. It declares a variable called @first_name. The @ prefix means that this is an instance variable which is visible from anywhere within the class. The name for this visibility is called scope. This concept will become clearer when we add some methods.
To declare a method in Ruby, we begin by using the keyword “def”, followed by the method name. In our case: def say_introduction. We then enter as many lines of code as are required to implement the desired behavior. Finally, we end the method definition with the keyword “end”. Notice that the same keyword, “end” is used to end both the class definition and method definitions.
Notice that we have moved the puts instruction from outside the class to inside the class. Now, when we want to print a greeting, we only need to call the method say_introduction on the object (referenced by a variable, a).
If we save our code in a file called “say_hello.rb” and run it using the ruby command, the output will appear as it does below:
Recall earlier when we described some behaviors as needing a bit more input, such as eating requires food? Also recall that we had a name for the additional information… argument or parameter.
We’re going to add a new method (behavior) to the Person class. We’re going to add the ability to say the greeting to a specific person. Look at the updated class definition below. We’ve created a new method, say_introduction_to, which expects a parameter to be passed to it. If you look at line 27, we do exactly that, passing to it the string “Joe”.
Let’s run this code and see the results below: