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.list_students(course)
: Returns an array of all students enrolled in a given course. If no students are enrolled, it returns an empty array.
Let’s implement each method one step at a time.
We’ll start by defining our EnrollmentSystem
class and initializing it with a hash to store enrollments.
This code sets up an empty courses_enrollment
hash, where each course will map to a set of students. Now that we have our class structure, let’s move to the methods.
The enroll
method adds a student to a course. If the course doesn’t exist yet in @courses_enrollment
, we’ll create a new set for it.
This method first checks if the course exists in @courses_enrollment
. If it doesn’t, @courses_enrollment[course] ||= Set.new
creates a new set for that course. Then, the student
is added to the course’s set, ensuring the student is only enrolled once.
We see here that after enrolling Alice in "Math101," the course is listed in @courses_enrollment
with Alice as the only student.
The unenroll
method removes a student from a course. If the course or student isn’t found, it returns false
. If the student is successfully removed, it also checks if the course is now empty and, if so, removes it.
Here, we first verify that both the course and the student exist in @courses_enrollment
. If they do, @courses_enrollment[course].delete(student)
removes the student from the course. If the course set becomes empty, @courses_enrollment.delete(course)
removes the course entry altogether. This method returns true
if the student was successfully unenrolled and false
otherwise.
In this example, Alice is enrolled and then successfully unenrolled. After Alice is removed, "Math101" no longer appears in @courses_enrollment
since it has no enrolled students.
The is_enrolled
method checks if a student is enrolled in a course, returning true
if they are and false
otherwise.
This method checks if both the course and the student exist in @courses_enrollment
. If they do, it returns true
; otherwise, it returns false
.
Here, the output confirms that Alice is enrolled in "Math101" but Bob is not.
Finally, the list_students
method returns an array of all students enrolled in a course. If no students are enrolled, it returns an empty array.
In this method, @courses_enrollment[course].to_a
converts the set of students to an array. If the course is not in @courses_enrollment
, it simply returns an empty array.
This example shows that both Alice and Bob are listed in "Math101," while an empty array is returned for "Physics101," as no one is enrolled in it.
Below is the complete implementation of the EnrollmentSystem
class combining all the methods we discussed:
This consolidated code provides a ready-to-use EnrollmentSystem
class complete with methods to manage student enrollments effectively.
Below are examples demonstrating how to use the methods in the EnrollmentSystem
class:
These examples demonstrate how to use the EnrollmentSystem
methods to enroll, check enrollment, list students, and unenroll students while maintaining the system's integrity.
In today’s lesson, we created an EnrollmentSystem
class using Ruby’s Set
and Hash
classes to manage student enrollments. We implemented methods to enroll and unenroll students, check if a student is enrolled, and list students in a course. This exercise demonstrates how to efficiently manage unique collections in Ruby using sets, especially in scenarios requiring efficient lookups and duplicate prevention.
Try extending this class with additional features or tackle similar challenges to deepen your understanding. Happy coding!
