-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhsq9plus.rb
76 lines (64 loc) · 1.25 KB
/
hsq9plus.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'rubygems' # Ruby 1.8用
require 'pebbles/sl'
class HQ9Plus
def initialize(src)
@src = src
@count = 0
end
def run
@src.each_char do |c|
case c
when "H"
hello
when "S"
run_sl
when "Q"
print_source
when "9"
print_99_bottles_of_beer
when "+"
increment
end
end
end
private
def hello
puts "Hello, world!"
end
def run_sl
Pebbles::SL.run
end
def print_source
print @src
end
def print_99_bottles_of_beer
99.downto(0) do |k|
case k
when 0
before = "no more bottles"
after = "99 bottles"
when 1
before = "1 bottle"
after = "no more bottles"
when 2
before = "2 bottles"
after = "1 bottle"
else
before = "#{k} bottles"
after = "#{k-1} bottles"
end
if k == 0
action = "Go to the store and buy some more"
else
action = "Take one down and pass it around"
end
puts "#{before.capitalize} of beer on the wall, #{before} of beer."
puts "#{action}, #{after} of beer on the wall."
puts "" unless k == 0
end
end
def increment
@count += 1
end
end
HQ9Plus.new(ARGF.read).run