Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created July 1, 2014 16:42
Show Gist options
  • Save jcasimir/5266daef766cceb080f4 to your computer and use it in GitHub Desktop.
Save jcasimir/5266daef766cceb080f4 to your computer and use it in GitHub Desktop.
class Garden
attr_reader :layout, :row_0, :row_1
def initialize(layout, two=nil)
@row_0, @row_1 = layout.split("\n")
@layout = layout.gsub("\n", "")
end
def alice
codes_to_plants( codes_for(:alice) )
end
def bob
codes_to_plants( codes_for(:bob) )
end
def charlie
codes_to_plants( codes_for(:charlie) )
end
def david
codes_to_plants( codes_for(:david) )
end
def eve
codes_to_plants( codes_for(:eve) )
end
def names
[:alice, :bob, :charlie, :david, :eve]
end
def codes_for(name)
position = names.index(name)
starter = 2 * position
ender = starter + 1
@row_0[starter..ender] + @row_1[starter..ender]
end
def codes_to_plants(codes)
codes.chars.collect{|char| code_to_plant(char)}
end
def code_to_plant(code)
case code
when "R" then :radishes
when "G" then :grass
when "V" then :violets
when "C" then :clover
else raise("Don't have a plant for #{code.inspect}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment