Skip to content

Instantly share code, notes, and snippets.

@tmichel
Created November 30, 2016 21:47
Show Gist options
  • Save tmichel/1c6364ba4279758e35cc0e15ab4bfa9a to your computer and use it in GitHub Desktop.
Save tmichel/1c6364ba4279758e35cc0e15ab4bfa9a to your computer and use it in GitHub Desktop.
The house that Jack built
class House
def self.recite
new.recite
end
attr_reader :start, :glue
def initialize(start: "This is", glue: "that")
@start = start
@glue = glue
end
def recite
pieces.size.times.map { |n| verse(n+1) }.join("\n")
end
def verse(n)
text = pieces.last(n).map.with_index do |(head, tail), idx|
if idx == 0
"#{start} #{tail}"
else
"#{glue} #{head} #{tail}"
end
end.join("\n")
text << "\n"
end
def pieces
[
[ "", "the horse and the hound and the horn" ],
[ "belonged to", "the farmer sowing his corn" ],
[ "kept", "the rooster that crowed in the morn" ],
[ "woke", "the priest all shaven and shorn" ],
[ "married", "the man all tattered and torn" ],
[ "kissed", "the maiden all forlorn" ],
[ "milked", "the cow with the crumpled horn" ],
[ "tossed", "the dog" ],
[ "worried", "the cat" ],
[ "killed", "the rat" ],
[ "ate", "the malt" ],
[ "lay in", "the house that Jack built." ],
]
end
end
require 'minitest/autorun'
require_relative 'house'
class HouseTest < Minitest::Test
def test_verse
house = House.new
{
1 => "This is the house that Jack built.\n",
2 => "This is the malt\nthat lay in the house that Jack built.\n",
}.each do |n, expected|
assert_equal expected, house.verse(n)
end
end
def test_different_start
house = House.new(start: "This is definitely")
assert_equal "This is definitely the house that Jack built.\n", house.verse(1)
end
def test_rhyme
expected = <<-RHYME
This is the house that Jack built.
This is the malt
that lay in the house that Jack built.
This is the rat
that ate the malt
that lay in the house that Jack built.
This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
RHYME
assert_equal expected, House.recite
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment