Question
Helen Hacker is creating a Ruby application to keep track of her favorite books and plays. She decides to create separate Ruby classes for Book
Helen Hacker is creating a Ruby application to keep track of her favorite books and plays. She decides to create separate Ruby classes for Book and Play, because even though both have a title and author, they are otherwise very different: for example, a Play has an opening_date, number_of_acts, and other attributes that don't apply to a Book. One function she needs to implement is to sort a list of books and plays by their title, that is, to sort a collection that may contain both instances of Book and instances of Play. What design should she use to solve this problem in Ruby?
a. a is of the correct class (type)
b. Since not all the elements to be sorted are of the same type, Helen must create a variant of Ruby's existing sort method (in the standard library) that can handle a mixed collection.
c. Book and Play must be subclasses of some common ancestor, such as Manuscript; this ancestor class should provide a sort method that can recognize and handle either the Book or Play subclass.
d. Since not all the elements to be sorted are of the same type, Helen must separate the collection into two collections (one containing only Books and the other containing only Plays), sort each collection, then recombine the two sorted collections.
e. As long as Book and Play both have an attribute named title, Helen can use the sort_by method to write something like sorted = list_of_works.sort_by { |work| work.title }.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started