Last active
March 29, 2016 16:41
-
-
Save justinxreese/60f298500b1a6d6544b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './roster_management' | |
set_roster | |
roster.each do |_, person| | |
next if person.teacher | |
person.display | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RosterManagement | |
class Person | |
attr_accessor :first_name, :last_name, :teacher | |
def initialize(atts) | |
@first_name = atts[:first_name] | |
@last_name = atts[:last_name] | |
@teacher = false | |
end | |
def it_me?(person_string) | |
first, last = person_string.split(' ') | |
first_name == first and last_name == last | |
end | |
def display | |
puts it_me?('Justin Reese') ? "Hey, that's me! #{first_name} #{last_name}" : "That's not me." | |
end | |
end | |
class Student < Person | |
end | |
class Teacher < Person | |
def initialize(atts) | |
super(atts) | |
@teacher = true | |
end | |
end | |
# https://gist.github.com/justinxreese | |
def store_person(name_string, teacher_status = false) | |
first, last = name_string.split(' ') | |
atts = {first_name: first, last_name: last} | |
person = teacher_status ? Teacher.new(atts) : Student.new(atts) | |
@roster.store(next_id, person) | |
end | |
def set_roster | |
@roster = {} | |
collect_teacher | |
collect_students | |
end | |
def collect_teacher | |
puts 'Enter the teacher' | |
teacher_input = gets | |
store_person(teacher_input, true) | |
end | |
def collect_students | |
puts 'Enter a student' | |
while student_input = gets | |
break if student_input.strip == 'stop' | |
store_person(student_input) | |
puts 'Enter a student or ^D to exit' | |
end | |
end | |
def next_id | |
@current_id = current_id + 1 | |
"id#{current_id}" | |
end | |
def current_id | |
@current_id ||= 0 | |
end | |
def roster | |
@roster | |
end | |
end | |
include RosterManagement |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment