Constantize to the Rescue!

The Problem and First Approach

I recently came across an interesting problem. I had a couple of classes Teacher and Student. Both classes had a method called .login that brings you to their respective login screens. While it may be inefficient, the user type is inputted by the user through a gets method.

user = gets.strip   # where user equals "Teacher" or "Student"
if user == "Teacher"
Teacher.login
elsif user == "Student"
Student.login
end
undefined method `login’ for “Student”:String
user.constantize.login

Magician, Mathematician, and Software Engineer