Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created June 13, 2013 15:35
Show Gist options
  • Save jcasimir/5774681 to your computer and use it in GitHub Desktop.
Save jcasimir/5774681 to your computer and use it in GitHub Desktop.
require 'active_support/core_ext/array'
class Garden
attr_reader :layout, :for_student, :students
def initialize(layout, students = Garden.default_students)
@students = students
@layout = parse(layout)
@for_student = group_by_student
define_student_lookups
end
def define_student_lookups
students.sort.each_with_index do |name, index|
Garden.send(:define_method, name.downcase) do
for_student[index]
end
end
end
def group_by_student
pods = layout.in_groups_of(2)
number_of_students = layout.size / 4
by_student = Array.new(number_of_students){ [] }
pods.each_with_index do |pod, index|
by_student[ index % number_of_students ] += pod
end
return by_student
end
def parse(layout)
codes = layout.split("").select{|code| code != "\n"}
codes.collect{|c| flower_for(c)}
end
def flower_for(code)
{"V" => :violets,
"C" => :clover,
"R" => :radishes,
"G" => :grass
}[code]
end
def self.default_students
%w(Alice Bob Charlie David Eve Fred
Ginny Harriet Ileana Joseph
Kincaid Larry)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment