Skip to content

Instantly share code, notes, and snippets.

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