- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Build a Library System with Ruby
In this Guided Code Lab, you will construct an object oriented "library system" with Ruby. You will design three core classes — Book, Borrower, and Library — to simulate fundamental library operations, such as adding books, checking them out, and returning them.
Lab Info
Table of Contents
-
Challenge
Introduction
In this Guided Code Lab, you will construct an object oriented "library system" with Ruby.
You will design three core classes —
Book,Borrower, andLibrary— to simulate fundamental library operations, such as adding books, checking them out, and returning them.Class Structures
Book Class
- The
Bookclass will represent individual books. Each book will have attributes such astitle,author, andavailable.
Borrower Class
- The
Borrowerclass will represent people who come to borrow books. EachBorrowerwill have attributes likenameandborrowed_books, which will hold a list of books currently borrowed by them.
Library Class
- The
Libraryclass will act as the main hub where the operations take place. This class will have attributes such asbook_list(a list of all books in the library) and methods to add books, check out books to borrowers, and receive returned books.
Interactions Between Objects
As you develop these classes, you'll also implement methods that allow these objects to interact. For example, when a borrower checks out a book, the
availabilityof the book changes, and the book gets added to the borrower'sborrowed_bookslist. Similarly, when a book is returned, itsavailabilitychanges again, and it gets removed from the borrower's list.When you are finished, click Run inside the Terminal to see the application's output.
If you're ready to get started, click the arrow below.
- The
-
Challenge
Creating a Book Class
The
Bookclass creates the books inside the library.Attributes:
titleauthoravailability
Methods:
initialize
All of your code will be written inside the
book.rbfile. -
Challenge
Creating a Borrower Class
The
Borrowerclass keeps track of who borrowed what books.Attributes:
nameborrowed_books
Let's get started!
All of your code will be written inside the
borrower.rbfile. -
Challenge
Creating a Library Class
The
Libraryclass will let you keep track of the inventory and peform all of the typical tasks of a library such as add books, checkout books, return books, and see what books are available.Attributes:
inventory
Methods:
initializeadd_bookcheckout_bookreturn_booklist_available_books
Let's get started!
All of your code will be written inside the
library.rbfile. -
Challenge
Library Methods
Overview
This section will cover the helper methods inside the
Libraryclass that give the app it's main functionality.Methods:
add_bookcheckout_bookreturn_booklist_available_books
Let's get started!
All of your code will be written inside the
library.rbfile. -
Challenge
Using the Library System
If you're classes are complete, you can click Run in the Terminal to see the following output:
The Great Gatsby has been checked out to John Doe. The Great Gatsby is not available. Available books: 1984 The Great Gatsby has been returned by John Doe. Available books: The Great Gatsby 1984Feel free to experiment, add books, check them out, and use the methods from the
Libraryclass.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.