beer_song.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
class BeerSong
  VERSION = 2

  def bottles(n)
    n = 'no more' if n == 0
    "#{n} bottle#{n == 1 ? '' : 's'} of beer"
  end

  def verse(n)
    line1 = "#{bottles(n).capitalize} on the wall, #{bottles(n)}.\n"
    if n > 0
      article = (n == 1 ? 'it' : 'one')
      line2 = "Take #{article} down and pass it around, #{bottles(n - 1)} on the wall.\n"
    else
      line2 = "Go to the store and buy some more, #{bottles(99)} on the wall.\n"
    end
    [line1, line2].join('')
  end

  def verses(m, n)
    m.downto(n).map { |i| verse(i) }.join("\n")
  end

  def lyrics
    verses(99, 0)
  end
end

Comments


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