-
-
Save sumproxy/5da24716ffe456c288dec44c4107e22e to your computer and use it in GitHub Desktop.
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 | |
STUDENTS = [:alice, :bob, :charlie, :david, | |
:eve, :fred, :ginny, :harriet, | |
:ileana, :joseph, :kincaid, :larry] | |
PLANTS = {"V" => :violets, | |
"C" => :clover, | |
"G" => :grass, | |
"R" => :radishes} | |
def initialize(diagram, students = STUDENTS) | |
students = students.map {|s| s.downcase.to_sym }.sort | |
row1, row2 = diagram | |
.lines | |
.map(&:chomp) | |
.map(&:each_char) | |
row1, row2 = row1.each_slice(2), row2.each_slice(2) | |
row1.zip(row2).each_with_index do |plants, idx| | |
self.class.send(:define_method, | |
students[idx], | |
-> { plants.flatten.map {|p| PLANTS[p] } }) | |
end | |
end | |
end |
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
#!/usr/bin/env ruby | |
gem 'minitest', '>= 5.0.0' | |
require 'minitest/autorun' | |
require_relative 'garden' | |
class TwoGardensDifferentStudents < Minitest::Test | |
def diagram | |
"VCRRGVRG\nRVGCCGCV" | |
end | |
def garden_1 | |
@garden_1 ||= Garden.new(diagram, %w(Alice Bob Charlie Dan)) | |
end | |
def garden_2 | |
@garden_2 ||= Garden.new(diagram, %w(Bob Charlie Dan Erin)) | |
end | |
def test_bob_and_charlie_per_garden | |
assert_equal [:radishes, :radishes, :grass, :clover], garden_1.bob | |
assert_equal [:violets, :clover, :radishes, :violets], garden_2.bob | |
assert_equal [:grass, :violets, :clover, :grass], garden_1.charlie | |
assert_equal [:radishes, :radishes, :grass, :clover], garden_2.charlie | |
end | |
end |
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
1) Failure: | |
TwoGardensDifferentStudents#test_bob_and_charlie_per_garden [garden_test.rb:128]: | |
--- expected | |
+++ actual | |
@@ -1 +1 @@ | |
-[:grass, :violets, :clover, :grass] | |
+[:radishes, :radishes, :grass, :clover] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment