Attribute Readers
This lesson explains attribute readers and how they’re related to instance variables.
We'll cover the following
Asking an object for information
Remember that people have the ability to remember their name, and tell it, when asked.
We’ve already implemented the first part of this. Our Person
instance now knows her name, “Ada”
.
Let’s look at the second part. Remember also that methods are either questions or commands. We want to add a method that answers the question, What’s your name?
It’s as simple as this:
class Persondef initialize(name)@name = nameenddef name@nameendend
Before we discuss what this does, let’s look at how we can use our new method. We can now call the method on the person
object, like this:
person = Person.new("Ada")puts person.name
This prints the name, Ada
, and that’s what we want. We can create a new person
object, passing a name to it. Once that person is created, we’re able to ask for its name, and we’ll get the name back.
Explanation
Let’s look at how this works internally:
-
In the first line, the object assigned to the
person
variable is an initialized new instance of thePerson
class. Initialized means that theinitialize
method has already been called and has already assigned the"Ada"
string to the@name
instance variable. -
Now, this
person
object has thename
method, as defined in the class definition ofPerson
above. In the second line, we call this method,person.name
. -
When the
name
method is called, it does nothing else but evaluate the@name
instance variable from the object scope. Because this has previously been set to"Ada"
, it returns this string. Because this is the last line in thename
method, the method also returns this string. -
For that reason, the
person.name
method call returns the"Ada"
string, which is then passed toputs
, which prints it out.
What are attribute readers?
Methods that do nothing else but return a value assigned to an instance variable with the same name, called attribute readers, are very common.
By attribute, the Ruby community means an instance variable, so an attribute reader is a method that reads an instance variable.
Remember: An attribute reader returns the value of an instance variable.
Another way of looking at this is that an attribute reader is a method that exposes an instance variable. That is, it makes it accessible for others. It allows us to ask for the value of an instance variable with the same name and does nothing but return its value. Once defined, others can ask this object for knowledge that otherwise would be private and inaccessible.
In our case, the name
attribute reader exposes the @name
instance variable so others can ask for it.
Remember: An attribute reader exposes an instance variable.
It’s unclear why “attribute” was the chosen term. It would be much less confusing to use the term “instance variable reader” instead. Maybe the simple reason is that programmers don’t like to type more than necessary, and this saves eight characters. Who knows?