What is an accessor?
We've discussed the real variables before, but not too much discussion. The real variable of an object belongs to its property and is a general difference from other objects from the same class. It is important to read and write its properties; This requires a property accessor (attribute accessors) Approach. We will soon see that we do not always have to explicitly write accessor methods, but let us now understand all the details. The two types of accessors are write (writer) and read (reader).
Ruby> class Fruit
| def set_kind (k) # a writer
| @kind = k
| End
| def Get_kind # a Reader
| @kind
| End
| End
Nil
Ruby> F1 = fruit.new
#<fruit:0xfd7e7c8c>
Ruby> f1.set_kind ("Peach") # Use the writer
"Peach"
ruby> F1.get_kind # Use the reader
"Peach"
Ruby> F1 # Inspect the object
#<fruit:0xfd7e7c8c @kind = "Peach" >
Simple enough; We have access to information about the kind of fruit we're searching for. But our method name is still a little grumbling. The following is simpler and more convenient.
Ruby> class Fruit
| def kind= (k)
| @kind = k
| End
| def kind
| @kind
| End
| End
Nil
ruby> F2 = fruit.new
#<fruit:0xfd7e7c8c>
ruby> f2.kind = "Banana"
"Banana"
Ruby> F2.kind
"Banana"
Inspect method
A little episode. You've noticed that when we try to look directly at an object, something like #<anobject:0x83678> will appear. It's just a default behavior, and we can change it freely. All we have to do is add a name called Inspect . It will change to a more explicit string that describes the object, including some or all of the real variables.
Ruby> class Fruit
| def inspect
| "A fruit of" + @kind + "variety"
| End
| End
Nil
ruby> F2
"A fruit of the banana variety"
A related method is to to_s (convert to a string) and use it when printing objects. Generally, you can think of inspect as a tool for writing or debugging a program, and to_s is a way to beautify the output of a program. EVAL.RB always use inspect when displaying results. You can use the P method to easily get debugging information from the program.
# These two lines are equivalent:
P AnObject
Print Anobject.inspect, "\ n"
Simple way to generate accessors
Because many real variables require access methods, Ruby provides an abbreviation for the standard method.
Shortcut abbreviation effect equivalent to
Attr_reader:v def v; @v; End
Attr_writer:v def v= (value); @v=value; End
Attr_accessor:v attr_reader:v; Attr_writer:v
Attr_accessor:v,: w attr_accessor:v; Attr_accessor:w
Let's use it to add "fresh" information. First, we automatically generate read and write methods, and then we merge this new information into the inspect:
Ruby> class Fruit
| Attr_accessor:condition
| def inspect
| "A" + @condition + @kind "
| End
| End
Nil
ruby> f2.condition = "ripe"
"Ripe"
ruby> F2
"A ripe banana"
More interesting fruit
If no one eats our ripe fruit, maybe we should let them rot away.
Ruby> class Fruit
| def time_passes
| @condition = "Rotting"
| End
| End
Nil
ruby> F2
"A ripe banana"
Ruby> f2.time_passes
"Rotting"
ruby> F2
"A rotting banana"
But when we do this, we introduce a small problem. Now, what happens if we create a third fruit? Remember that a real variable does not exist before the assignment.
ruby> F3 = Fruit.new
Err:failed to convert nil into String
It's a inspect way to complain about it here. We have let it report the variety and status of the fruit, but F3 has not yet assigned any value. If we want, can we rewrite the inspect method to use define? Methods test real variables and report only when they exist, but that may not be useful, because each fruit has a type and a state. It seems that we should be certain of its attributes. That's what we're going to discuss in the next section.