/ruby/food-chain/food_chain.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class FoodChainSong
  CREATURES = [
    'fly',
    'spider',
    'bird',
    'cat',
    'dog',
    'goat',
    'cow',
    'horse',
  ]

  EXTRAS = {
    'spider' => 'It 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!',
    'horse' => 'She\'s dead, of course!'
  }

  def verse(n)
    creature = CREATURES[n-1]

    lines = []
    lines.push("I know an old lady who swallowed a #{creature}.")

    extra = EXTRAS[creature]
    if extra
      lines.push(extra)
    end

    if creature != 'horse'
      (n-1).downto(1).each do |i|
        predator = CREATURES[i]
        prey = CREATURES[i-1]

        wriggled = (prey == 'spider' ?
                    ' that wriggled and jiggled and tickled inside her' :
                    '')
        lines.push("She swallowed the #{predator} to catch the #{prey}#{wriggled}.")
      end

      lines.push("I don't know why she swallowed the fly. Perhaps she'll die.")
    end


    lines.join("\n") + "\n"
  end

  def verses(first, last)
    # All verses between first and last, inclusive
    # (using a 1-based index)
    first.upto(last).collect { |i| verse(i) }.join("\n") + "\n"
  end

  def sing
    # Sing all the verses
    verses(1, CREATURES.length)
  end
end

Comments


You're not logged in right now. Please login via GitHub to comment