Introduction

Welcome to today’s lesson! We’ll explore how to use Ruby’s Set class in a practical context by creating a student enrollment system for courses. Imagine you’re managing enrollments for an online course platform, where students enroll in courses, check their enrollments, or get a list of classmates. Using Set is ideal here since it automatically prevents duplicates, ensuring each student can only enroll in a course once.

By the end of this lesson, you’ll have a solid understanding of how to use sets and hashes for handling unique collections. Let’s get started!

Introducing Methods to Implement

In this system, we’ll build four methods for managing student enrollments:

  • enroll(student, course): Adds a student to a course. If the student is already enrolled, it does nothing.
  • unenroll(student, course): Removes a student from a course. Returns true if the student was enrolled and has now been removed, otherwise returns false. If the course becomes empty after unenrolling the student, it should also be removed from the system.
  • is_enrolled(student, course): Checks if a student is enrolled in a course, returning true if they are and false otherwise.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal