Created
July 1, 2014 17:03
-
-
Save jcasimir/c6158d4a4dd3f6145fee to your computer and use it in GitHub Desktop.
Full Garden.rb with Metaprogramming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Garden | |
attr_reader :row_0, :row_1, :names | |
def initialize(layout, names=nil) | |
@row_0, @row_1 = layout.split("\n") | |
@names = format_names(names) || default_names | |
build_name_accessor_methods | |
end | |
def format_names(names) | |
names.collect{|name| name.downcase.to_sym}.sort if names | |
end | |
def default_names | |
[:alice, :bob, :charlie, :david, :eve, :fred, :ginny, | |
:harriet, :ileana, :joseph, :kincaid, :larry] | |
end | |
def build_name_accessor_methods | |
names.each_with_index do |name, position| | |
define_singleton_method(name) do | |
codes_to_plants( codes_for(position) ) | |
end | |
end | |
end | |
def codes_for(position) | |
start_at = 2 * position | |
end_at = start_at + 1 | |
@row_0[start_at..end_at] + @row_1[start_at..end_at] | |
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