Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

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.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 5m
Published
Clock icon Aug 31, 2023

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. 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, and Library — to simulate fundamental library operations, such as adding books, checking them out, and returning them.

    Class Structures

    Book Class

    • The Book class will represent individual books. Each book will have attributes such as title, author, and available.

    Borrower Class

    • The Borrower class will represent people who come to borrow books. Each Borrower will have attributes like name and borrowed_books, which will hold a list of books currently borrowed by them.

    Library Class

    • The Library class will act as the main hub where the operations take place. This class will have attributes such as book_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 availability of the book changes, and the book gets added to the borrower's borrowed_books list. Similarly, when a book is returned, its availability changes 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.

  2. Challenge

    Creating a Book Class

    The Book class creates the books inside the library.

    Attributes:

    • title
    • author
    • availability

    Methods:

    • initialize

    All of your code will be written inside the book.rb file.

  3. Challenge

    Creating a Borrower Class

    The Borrower class keeps track of who borrowed what books.

    Attributes:

    • name
    • borrowed_books

    Let's get started!

    All of your code will be written inside the borrower.rb file.

  4. Challenge

    Creating a Library Class

    The Library class 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:

    • initialize
    • add_book
    • checkout_book
    • return_book
    • list_available_books

    Let's get started!

    All of your code will be written inside the library.rb file.

  5. Challenge

    Library Methods

    Overview

    This section will cover the helper methods inside the Library class that give the app it's main functionality.

    Methods:

    • add_book
    • checkout_book
    • return_book
    • list_available_books

    Let's get started!

    All of your code will be written inside the library.rb file.

  6. 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
    1984
    

    Feel free to experiment, add books, check them out, and use the methods from the Library class.

Danny Sullivan is a former special education teacher and professional baseball player that moved into software development in 2014. He’s experienced with Ruby, Python and JavaScript ecosystems, but enjoys Ruby most for its user friendliness and rapid prototyping capabilities.

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.