-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathtwelve_days.rb
36 lines (35 loc) · 1.13 KB
/
twelve_days.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
#Problem: https://exercism.org/tracks/ruby/exercises/twelve-days
#Solution
class TwelveDays
DAYS = {
1=>["first","a Partridge in a Pear Tree"],
2=>["second","two Turtle Doves"],
3=>["third","three French Hens"],
4=>["fourth","four Calling Birds"],
5=>["fifth","five Gold Rings"],
6=>["sixth","six Geese-a-Laying"],
7=>["seventh","seven Swans-a-Swimming"],
8=>["eighth","eight Maids-a-Milking"],
9=>["ninth","nine Ladies Dancing"],
10=>["tenth","ten Lords-a-Leaping"],
11=>["eleventh","eleven Pipers Piping"],
12=>["twelfth","twelve Drummers Drumming"]
}
def self.song
song = ""
base_line = "On the %{ith} day of Christmas my true love gave to me: %{phrase}.\n"
for i in 1..12
phrase = ""
j =i
while j>0
phrase+="and " if j==1 and i!=1
phrase+=DAYS[j][1]
phrase+=", " unless j==1
j-=1
end
song+="\n" unless song==""
song+= base_line % {ith:DAYS[i][0],phrase:phrase}
end
song
end
end