Accessor Methods

Although the classes in this would-be adventure game work well enough, they are still fairly verbose because of all those get and set accessors. Let’s see what you can do to remedy this.

Instead of accessing the value of the @description instance variable with two different methods, get_description and set_description, like this:

puts( t1.get_description )
t1.set_description("Some description" )

it would be so much nicer to retrieve and assign values just as you would retrieve and assign values to and from a simple variable, like this:

puts( t1.description )
t1.description = "Some description"

To be able to do this, you need to modify the Treasure class definition. One way of accomplishing this would be to rewrite the accessor methods ...

Get The Book of Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.