forked from sleepyfox/code-dojo-39
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsong.rb
More file actions
59 lines (57 loc) · 1.37 KB
/
song.rb
File metadata and controls
59 lines (57 loc) · 1.37 KB
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
def sing_verse(stanzas, delimiter)
if stanzas.length == 1
puts "There was an old lady who swallowed a #{stanzas.last[:animal]}#{delimiter}"
puts stanzas.last[:line]
else
puts "There was an old lady who swallowed a #{stanzas.last[:animal]};"
puts stanzas.last[:line]
lines = (1..stanzas.length-1).reverse_each.map do |index|
"She swallowed the #{stanzas[index][:animal]} to catch the #{stanzas[index-1][:animal]}"
end
puts "#{lines.join(",\n")};"
puts stanzas.first[:line]
end
end
def sing_song(stanzas)
unless stanzas.empty?
(0..stanzas.length-2).each do |index|
sing_verse(stanzas.slice(0..index), ".")
puts ""
end
sing_verse([stanzas.last], "...")
end
end
if ARGV.empty?
sing_song([
{
animal: "fly",
line: "I don't know why she swallowed a fly - perhaps she'll die!"
},
{
animal: "spider",
line: "That wriggled and wiggled and tickled inside her."
},
{
animal: "bird",
line: "How absurd to swallow a bird."
},
{
animal: "cat",
line: "Fancy that to swallow a cat!"
},
{
animal: "dog",
line: "What a hog, to swallow a dog!"
},
{
animal: "cow",
line: "I don't know how she swallowed a cow!"
},
{
animal: "horse",
line: "...She's dead, of course!"
},
])
else
sing_song(eval(ARGV[0]))
end