ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# remcopeereboom's solution

## to Food Chain in the Ruby Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.

While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.

This is a cumulative song of unknown origin.

This is one of many common variants.

``````I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a spider.
It wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a bird.
How absurd to swallow a bird!
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cat.
Imagine that, to swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a dog.
What a hog, to swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a horse.
``````

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby food_chain_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride food_chain_test.rb
``````

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### food_chain_test.rb

``````require 'minitest/autorun'

require_relative 'food_chain'

class NoCheating < IOError
def message
"The use of File.open and IO.read is restricted.\n"                \
'the song.txt file.'
end
end

class FoodChainTest < Minitest::Test
# This test is an acceptance test.
#
# If you find it difficult to work the problem with so much
# to make sure you got it right.
# There's no need to submit the tests you write, unless you
# specifically want feedback on them.
def test_the_whole_song
song_file = File.expand_path('../song.txt', __FILE__)
assert_equal expected, FoodChain.song
end

# Tests that an error is effectively raised when IO.read or
# File.open are used within FoodChain.
song_file = File.expand_path('../song.txt', __FILE__)
["IO.read '#{song_file}'", "File.open '#{song_file}'"].each do |trigger|
assert_raises(NoCheating) { FoodChain.send :class_eval, trigger }
end
end

# Problems in exercism evolve over time,
# as we find better ways to ask questions.
# The version number refers to the version of the problem you solved,
#
# Define a constant named VERSION inside of BookKeeping.
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_version
skip
assert_equal 2, BookKeeping::VERSION
end
end

module RestrictedClasses
class File
def self.open(*)
fail NoCheating
end

fail NoCheating
end

def open(*)
fail NoCheating
end

fail NoCheating
end
end

class IO
fail NoCheating
end

fail NoCheating
end
end
end

FoodChain.prepend RestrictedClasses``````
``````class FoodChainSong
def verse(n)
if ANIMALS.length == n
death_verse
else
regular_verse n
end
end

def verses(first, last)
lyrics = ''
first.upto(last) { |i| lyrics << verse(i) << "\n" }
lyrics
end

def sing
verses(1, ANIMALS.length)
end

private
ANIMALS = %w{fly spider bird cat dog goat cow horse}
FLAVOR_LINES = ["",
"\nIt wriggled and jiggled and tickled inside her.",
"\nHow absurd to swallow a bird!",
"\nImagine that, to swallow a cat!",
"\nWhat a hog, to swallow a dog!",
"\nJust opened her throat and swallowed a goat!",
"\nI don't know how she swallowed a cow!"]

def regular_verse(n)
lyrics = ''
lyrics << "I know an old lady who swallowed a #{ANIMALS[n - 1]}."
lyrics << FLAVOR_LINES[n - 1]
n.downto(2) { |i| lyrics << swallow(i) }
lyrics << "\nI don't know why she swallowed the fly. Perhaps she'll die.\n"
end

def death_verse
"I know an old lady who swallowed a #{ANIMALS.last}." +
end

def swallow(n)
if 3 == n
"\nShe swallowed the #{ANIMALS[n - 1]} to catch the #{ANIMALS[n - 2]}" +
" that wriggled and jiggled and tickled inside her."
else
"\nShe swallowed the #{ANIMALS[n - 1]} to catch the #{ANIMALS[n - 2]}."
end
end
end``````

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?