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!
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. Returnstrue
if the student was enrolled and has now been removed, otherwise returnsfalse
. 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, returningtrue
if they are andfalse
otherwise.
