Hi all,
While doing an exercism
(http://exercism.io/exercises/ruby/kindergarten-garden/readme)
I wrote the code below:
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 code passes all the tests except for this one
#!/usr/bin/env ruby
gem ‘minitest’, ‘>= 5.0.0’
require ‘minitest/autorun’
require_relative ‘kindergarten_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
And I can’t find the reason why. If asserts in
test_bob_and_charlie_per_garden are wrapped in a proper test method
each, then the test succeeds