Created
July 15, 2015 16:28
-
-
Save noili/ac48914c607bfcae5bc4 to your computer and use it in GitHub Desktop.
food_chain for exercism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'pry' | |
| class FoodChain | |
| ANIMALS = {'fly' => 'I don\'t know why she swallowed the fly. Perhaps she\'ll die.', | |
| 'spider' => 'that wriggled and jiggled and tickled inside her.', | |
| 'bird' => 'How absurd to swallow a bird!', | |
| 'cat' => 'Imagine that, to swallow a cat!', | |
| 'dog' => 'What a hog, to swallow a dog!', | |
| 'goat' => 'Just opened her throat and swallowed a goat!', | |
| 'cow' => 'I don\'t know how she swallowed a cow!' | |
| } | |
| def self.song | |
| @song = '' | |
| ANIMALS.size.times do |i| | |
| @song += <<-cancion | |
| #{stanzas(i)} | |
| cancion | |
| end | |
| binding.pry | |
| end | |
| def self.stanzas i | |
| return first_stanza if i == 0 | |
| return second_stanza if i == 1 | |
| @stanza = '' | |
| @stanza = <<-stanza | |
| I know an old lady who swallowed a #{ANIMALS.keys[i]}. | |
| #{ANIMALS[ANIMALS.keys[i]]} | |
| #{swallowing_chain(i)} | |
| #{ANIMALS[ANIMALS.keys[0]]} | |
| stanza | |
| end | |
| def self.swallowing_chain c | |
| return spider_verse if c == 2 | |
| @chain = '' | |
| @chain = <<-chain | |
| She swallowed the #{ANIMALS.keys[c]} to catch the #{ANIMALS.keys[c-1]} | |
| #{swallowing_chain(c-1)} | |
| chain | |
| end | |
| def self.first_stanza | |
| @stanza = '' | |
| @stanza = <<-stanza | |
| I know an old lady who swallowed a #{ANIMALS.keys[0]}. | |
| #{ANIMALS[ANIMALS.keys[0]]} | |
| stanza | |
| end | |
| def self.second_stanza | |
| @stanza = '' | |
| @stanza = <<-stanza | |
| I know an old lady who swallowed a #{ANIMALS.keys[1]}. | |
| It wriggled and jiggled and tickled inside her. | |
| She swallowed the #{ANIMALS.keys[1]} to catch the #{ANIMALS.keys[0]} | |
| #{ANIMALS[ANIMALS.keys[0]]} | |
| stanza | |
| end | |
| def self.spider_verse | |
| "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\nShe swallowed the spider to catch the fly." | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment