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

# angelikatyborska'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 FoodChain
VERSION = 2
PROGRESSION_STEPS = [
{
animal: 'fly',
description: 'I don\'t know why she swallowed the fly. Perhaps she\'ll die.'
},
{
animal: 'spider',
description: 'It wriggled and jiggled and tickled inside her.'
},
{
animal: 'bird',
description: 'How absurd to swallow a bird!'
},
{
animal: 'cat',
description: 'Imagine that, to swallow a cat!'
},
{
animal: 'dog',
description: 'What a hog, to swallow a dog!'
},
{
animal: 'goat',
description: 'Just opened her throat and swallowed a goat!'
},
{
animal: 'cow',
description: 'I don\'t know how she swallowed a cow!'
},
{
animal: 'horse',
}
]

class << self
def song
song = ''

PROGRESSION_STEPS.length.times do |verse_number|
song << "\n" unless song == ''
song << verse(verse_number)
end

song
end

def verse(n)
verse = the_beginning(PROGRESSION_STEPS[n])
verse << the_rest(n) if n < PROGRESSION_STEPS.length - 1
verse
end

private

def the_beginning(step)
lines = "I know an old lady who swallowed a #{ step[:animal] }.\n"
lines << "#{ step[:description] }\n"
end

def the_rest(n)
lines = ''

PROGRESSION_STEPS[0..n].reverse.each_cons(2) do |effect, cause|
lines << middle_lines(effect, cause)
end

lines << the_last_line if n > 0
lines
end

def middle_lines(effect, cause)
lines = "She swallowed the #{ effect[:animal] } to catch the #{ cause[:animal] }"

lines <<
if cause == PROGRESSION_STEPS[1]
" #{ cause[:description].gsub(/^\w+/, 'that') }"
else
'.'
end

lines << "\n"
end

def the_last_line
"#{ PROGRESSION_STEPS[0][:description] }\n"
end
end
end``````