Skip to content

Instantly share code, notes, and snippets.

@johaywood
Created September 16, 2015 00:33
Show Gist options
  • Save johaywood/278a3ef62bd520c31da2 to your computer and use it in GitHub Desktop.
Save johaywood/278a3ef62bd520c31da2 to your computer and use it in GitHub Desktop.
Secret Santa Exercise
class SecretSanta
attr_accessor :people
def initialize(file)
@people = []
File.new(file).each_line do |line|
first, last = line.split(' ')
@people << Person.new(first, last)
end
assign_santas
end
def assign_santas
print '.'
@people.shuffle.each_with_index do |person, i|
@people[i].ss_for = person
end
end
def valid?
@people.all? do |person|
person.last_name != person.ss_for.last_name
end
end
end
class Person
attr_accessor :first_name, :last_name, :ss_for
def initialize(first, last)
@first_name = first
@last_name = last
end
def to_s
"#{first_name} #{last_name}"
end
end
if __FILE__ == $0
santas = SecretSanta.new('people.txt')
santas.assign_santas until santas.valid?
puts
santas.people.each do |person|
printf "%20s is a secret santa for %-20s\n", person.ss_for, person
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment