-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfizzbuzz.rb
More file actions
58 lines (47 loc) · 906 Bytes
/
fizzbuzz.rb
File metadata and controls
58 lines (47 loc) · 906 Bytes
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
require "json"
class Fizzy
def initialize(max)
@fizz_buzzed_array = (1..max).map do |num|
fizz_single(num)
end
end
def to_a
@fizz_buzzed_array
end
def to_s
@fizz_buzzed_array.join(",")
end
def to_html
collector = "<ul>\n"
@fizz_buzzed_array.each do |num|
collector += "<li>#{num}</li>\n"
end
collector += "</ul>\n"
end
def to_json
JSON.generate(@fizz_buzzed_array)
end
def fizz_single(num)
if !num.is_a?(Numeric)
raise ArgumentError.new("The number must be a numeric value - currently = #{num}")
elsif num < 0
raise ArgumentError.new("The number must be non negitive = currently = #{num}")
elsif num % 15 == 0
"FizzBuzz"
elsif num % 5 == 0
"Buzz"
elsif num % 3 == 0
"Fizz"
else
num.to_s
end
end
end
if __FILE__ == $0
f = Fizzy.new(30)
puts f
puts f.to_html
puts f.to_json
f2 = Fizzy.new(15)
puts f2.to_a
end