# \#126 \[Solution\] The varieties of FizzBuzz experience

**URL:** <https://www.ruby-forum.com/t/126-solution-the-varieties-of-fizzbuzz-experience/105835>\
**Category:** Ruby\
**Created:** [June 3, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/126-solution-the-varieties-of-fizzbuzz-experience/105835 "2007-06-03T16:51:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [June 3, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/126-solution-the-varieties-of-fizzbuzz-experience/105835/1 "2007-06-03T16:51:14Z")

</div>

Here are ten different solutions:

class Integer

# RubyQuiz 126 Solutions by Rick DeNatale

# 

# This quiz was simple enough that I decided to come up with as many

different ways to

# do it some simple, some rather bizzare and cryptic.

#This one simply follows the spec pretty slavishly.  
def fizz\_buzz1  
case  
when self % 15 == 0  
“FizzBuzz”  
when self % 3 == 0  
“Fizz”  
when self % 5 == 0  
“Buzz”  
else  
self.to\_s  
end  
end

# Nest the fizzbuzz test

def fizz\_buzz2  
case  
when self % 3 == 0  
self % 5 == 0 ? “FizzBuzz” : “Fizz”  
when self % 5 == 0  
“Buzz”  
else  
self.to\_s  
end  
end

# basically the same as 4 but using interpolation, and the fact that

# 1. An array returns nil for oob indices, and

# 3. nil.to\_s returns the empty string.

def fizz\_buzz3  
case  
when self % 3 == 0  
“Fizz#{[“Buzz”][self % 5]}”  
when self % 5 == 0  
“Buzz”  
else  
self.to\_s  
end  
end

# Use a mod 15 approach

def fizz\_buzz4  
([:FizzBuzz, nil, nil, :Fizz, nil, :Buzz, :Fizz, nil, nil, :Fizz,  
:Buzz, nil, :Fizz][self % 15]||self).to\_s  
end

# Kids don’t try this at home

# The to\_s(15) makes the last character wholly dependent on self % 15

# Then a series of subs are used to evolve the string to the correct

answer.  
def fizz\_buzz5  
(“%2d,%s” % [self,  
to\_s(15)]).sub(/.+?[369c]$/,‘Fizz’).sub(/.+?[0]$/,‘FizzBuzz’).sub(/.+?[5a]$/,“Buzz”).sub(/^\s?(\d+),.\*$/,‘\1’)  
end

# This was really a stretch for me, since it seems the most cryptic.

# And I HATE cryptic code.

def fizz\_buzz6  
(“%2d,%s” % [self, to\_s(15)]).sub(/.+?((0|([5a]))|[369c])$/) {  
“#{$3 ? ‘’ : ‘Fizz’}#{$2 ? ‘Buzz’ : ‘’}”  
}.sub(/^\s?(\d+),.\*$/,‘\1’)  
end

# Here’s another one-line method approach

def fizz\_buzz7  
(self % 15 == 0) ? “FizzBuzz” : ((self % 3 == 0) ? “Fizz” : ((self  
% 5 == 0) ? “Buzz” : to\_s))  
end

# And one more

def fizz\_buzz8  
“#{“Fizz” if self % 3 == 0}#{“Buzz” if self % 5 == 0}#{[nil,  
self][1+((self % 3)\*(self % 5))]}”  
end  
end

# Hand coded result for testing.

# This is arranged in rows of 15 showing the mod 15 pattern.

expected = %w{1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14  
FizzBuzz  
16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29  
FizzBuzz  
31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz  
46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz  
61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz  
76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz  
91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz  
}

# test em all, these should all print true. Yeah, I know these should

be test::unit testcases.  
p (1…100).map {|i| i.fizz\_buzz1} == expected  
p (1…100).map {|i| i.fizz\_buzz2} == expected  
p (1…100).map {|i| i.fizz\_buzz3} == expected  
p (1…100).map {|i| i.fizz\_buzz4} == expected  
p (1…100).map {|i| i.fizz\_buzz5} == expected  
p (1…100).map {|i| i.fizz\_buzz6} == expected  
p (1…100).map {|i| i.fizz\_buzz7} == expected  
p (1…100).map {|i| i.fizz\_buzz8} == expected

# and now two one liners

p (1…100).map {|i| (i % 15 == 0) ? “FizzBuzz” : ((i % 3 == 0) ?  
“Fizz” : ((i % 5 == 0) ? “Buzz” : i.to\_s))} == expected  
p (1…100).map {|i| “#{“Fizz” if i % 3 == 0}#{“Buzz” if i % 5 ==  
0}#{[nil, i][1+((i % 3)\*(i % 5))]}”} == expected

# Now lets use one of these to actually print

(1…100).each {|i| p i.fizz\_buzz1}

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
