Skip to content

Instantly share code, notes, and snippets.

@kennethgeerts
Created April 15, 2016 05:27
Show Gist options
  • Save kennethgeerts/d211b9246d462f632202fd45cf75dfc6 to your computer and use it in GitHub Desktop.
Save kennethgeerts/d211b9246d462f632202fd45cf75dfc6 to your computer and use it in GitHub Desktop.
class FoodChain
VERSION = 2
def self.song
0.upto(7).map { |n| FoodChainStanza.nth(n) }.join("\n")
end
end
class FoodChainStanza
ANIMALS = %w(fly spider bird cat dog goat cow horse)
SENTENCES = [
"",
"It wriggled and jiggled and tickled inside her.\n",
"How absurd to swallow a bird!\n",
"Imagine that, to swallow a cat!\n",
"What a hog, to swallow a dog!\n",
"Just opened her throat and swallowed a goat!\n",
"I don't know how she swallowed a cow!\n",
"She's dead, of course!\n"
]
def self.nth(n)
self.new(n).to_s
end
def initialize(n)
@n = n
end
def to_s
result = intro
result << continuation if @n < 7
result
end
private
def intro
result = "I know an old lady who swallowed a #{ANIMALS[@n]}.\n"
result << SENTENCES[@n]
end
def continuation
result = @n.downto(1).map { |i| middle_sentence(i) }.join
result << ending
end
def middle_sentence(i)
result = "She swallowed the #{ANIMALS[i]} to catch the #{ANIMALS[i - 1]}"
result << ' that wriggled and jiggled and tickled inside her' if i == 2
result << ".\n"
end
def ending
"I don't know why she swallowed the fly. Perhaps she'll die.\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment