For today's challenge we are going to act as librarians that are trying to keep track of our books.
First, we need to clone the repository and then run irb in the terminal.
-
Create a
Book
class (in a file calledbook.rb
) so that we can track how many copies of a book we have and whether we have personally finished reading it or not.- The
Book
class should havetitle
,author
,finished
, andcount
instance variables. - You should be able to pass in the
title
andauthor
values when you instantiate a book, but only those two attributes.- Example:
Book.new("Practical Object-Oriented Design in Ruby", "Sandi Metz")
.
- Example:
finished
is a boolean and should default tofalse
.count
is an integer and should default to3
.- All of the instance variables should be set in the initialize method.
- The
-
Test out your book class using irb.
-
Use
attr_
macros to provide access to the instance variables.title
andauthor
should only be readable.finished
should only be writeable.count
should be readable and writeable.
-
Create
AudioBook
(audio_book.rb
) andComicBook
(comic_book.rb
) classes that inherit fromBook
.- Add a
listen
method toAudioBook
that sets thefinished
value totrue
. - Add a
read
method toComicBook
that sets thefinished
value totrue
- Add a
-
Add a
recommended_books
class method to theBook
class that creates and returns an array of recommended books- Add the following code inside of the
recommended_books
class method:
- Add the following code inside of the
[
Book.new("The Well-Grounded Rubyist", "David A. Black"),
Book.new("Practical Object-Oriented Design in Ruby", "Sandi Metz"),
Book.new("Effective Testing with RSpec 3", "Myron Marston"),
]
- Test out the
recommended_books
class method
-
Create a
Lendable
module with alend
method that decrements the currentcount
by one.- Prevent the
count
from going below0
in thelend
method - Mixin the module into
Book
- Test that you can lend a book by verifying the count has reduced
- Prevent the