Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Last active February 14, 2018 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubysolo/e1dc6dfcd670bf28b876f2adaf642244 to your computer and use it in GitHub Desktop.
Save rubysolo/e1dc6dfcd670bf28b876f2adaf642244 to your computer and use it in GitHub Desktop.
class Garden
DEFAULT_STUDENTS = %w(
Alice Bob Charlie David
Eve Fred Ginny Harriet
Ileana Joseph Kincaid Larry
)
def initialize(garden_order, students=DEFAULT_STUDENTS)
@garden_order = garden_order
@students = students.map { |student| student.downcase }.sort
@plant_hash = {
V: :violets,
C: :clover,
R: :radishes,
G: :grass
}
@students.each_with_index do |student, index|
define_singleton_method student.to_sym, -> {
convert_to_plants(index * 2)
}
end
end
def garden_rows(position)
@garden_order.split("\n").map do |row|
row.split("").drop(position).take(2)
end.flatten
end
def convert_to_plants(position)
garden_rows(position).map do |plant|
@plant_hash[plant.to_sym]
end
end
end
class Garden
DEFAULT_STUDENTS = %w(
Alice Bob Charlie David
Eve Fred Ginny Harriet
Ileana Joseph Kincaid Larry
)
def initialize(garden_order, students=DEFAULT_STUDENTS)
@garden_order = garden_order
@students = students.map { |student| student.downcase }.sort
@plant_hash = {
V: :violets,
C: :clover,
R: :radishes,
G: :grass
}
end
def garden_rows(position)
@garden_order.split("\n").map do |row|
row.split("").drop(position).take(2)
end.flatten
end
def method_missing(method_id, *args)
student_place = @students.index(method_id.to_s)
convert_to_plants(student_place * 2)
end
def convert_to_plants(position)
garden_rows(position).map do |plant|
@plant_hash[plant.to_sym]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment