Book Shelf

Published by Helen Yu in

Create a Book class that has two accessors :

  1. title
  2. author

and two additional methods:

  1. A method named getTitle that returns: "Title: " + the instance title.
  2. A method named getAuthor that returns: "Author: " + the instance author.

and instantiate this constructor by creating 3 new books:

  1. Pride and Prejudice - Jane Austen (pp)
  2. Hamlet - William Shakespeare (h)
  3. War and Peace - Leo Tolstoy (wp)

The name of the new hash instances pp, h, and wp, respectively.

For instance, if I instantiated the following book:

  • Harry Potter - J.K. Rowling (hp)

I would get the following properties and methods:

Examples

hp.title ➞ "Harry Potter"
hp.author ➞ "J.K. Rowling"
hp.get_title ➞ "Title: Harry Potter"
hp.get_author ➞ "Author: J.K. Rowling"

Notes

  • Read more about classes in Ruby, view Resources.
  • Remember, after you've finished writing the class, you must instantiate it through the creation of new instances.
Watch a quick demo on how Edabit works.