July 2011
Intermediate to advanced
400 pages
8h 46m
English
In fact, there is a simpler and shorter way of creating a pair of get and set accessors simultaneously. All you have to do is use two special methods, attr_reader and attr_writer, followed by a symbol (a name preceded by a colon):
attr_reader :description attr_writer :description
You should add this code inside your class definition like this:
class Thing
attr_reader :description
attr_writer :description
# maybe some more methods here...
endCalling attr_reader with a symbol has the effect of creating a get accessor (here named description) for an instance variable (@description) with a name matching the symbol (:description).
Calling attr_writer similarly creates a set accessor for an instance variable. Instance variables ...
Read now
Unlock full access