# Re: \[QUIZ\] FizzBuzz (#126)

**URL:** <https://www.ruby-forum.com/t/re-quiz-fizzbuzz-126/106082>\
**Category:** Ruby\
**Created:** [June 6, 2007, 7:11am UTC](https://www.ruby-forum.com/t/re-quiz-fizzbuzz-126/106082 "2007-06-06T07:11:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 6, 2007, 7:11am UTC](https://www.ruby-forum.com/t/re-quiz-fizzbuzz-126/106082/1 "2007-06-06T07:11:04Z")

</div>

If I wanted to show them how eXtreme I was, I would start by writing  
some tests:

* * *

# fizzbuzz\_test.rb

require ‘spec’  
require ‘fizzbuzz’

describe :fizzbuzz do

# This is just so we can read the results of all those ‘puts’

before do  
@output = StringIO.new()  
$stdout = @output  
fizzbuzz()  
@output.rewind  
@lines = @output.readlines.map {|str| str.chop}  
end

after do  
$stdout = STDOUT  
end

it “should print 1 as the first line” do  
@lines[0].to\_i.should == 1  
end

## end

Then we make it pass

* * *

# fizzbuzz.rb

def fizzbuzz()  
puts 1  
end

## if **FILE** == $0 fizzbuzz() end

Add another test

* * *

# fizzbuzz\_test.rb

## it “should have 100 lines” do @lines.size.should == 100 end

Make it pass

* * *

# fizzbuzz.rb

## def fizzbuzz() (1…100).each do |num| puts num end end

Keep the rhythm going

* * *

# fizzbuzz\_test.rb

## it “should print ‘Fizz’ as the third line” do @lines[2].should == ‘Fizz’ end

# fizzbuzz.rb

## def fizzbuzz() (1…100).each do |num| if num % 3 == 0 puts ‘Fizz’ else puts num end end end

# fizzbuzz\_test.rb

## it “should print ‘Buzz’ as the fifth line” do @lines[4].should == ‘Buzz’ end

# fizzbuzz.rb

## def fizzbuzz() if num % 3 == 0 puts ‘Fizz’ elsif num % 5 == 0 puts ‘Buzz’ else puts num end end end

# fizzbuzz\_test.rb

## it “should print ‘FizzBuzz’ as the fifteenth line” do @lines[14].should == ‘FizzBuzz’ end

# fizzbuzz.rb

## def fizzbuzz() (1…100).each do |num| if num % 15 == 0 puts ‘FizzBuzz’ elsif num % 3 == 0 puts ‘Fizz’ elsif num % 5 == 0 puts ‘Buzz’ else puts num end end end

And one more for good measure:

* * *

# fizzbuzz\_test.rb

## it “should print ‘Buzz’ as the last line” do @lines.last.should == ‘Buzz’ end

The final results are

* * *

# fizzbuzz.rb

def fizzbuzz()  
(1…100).each do |num|  
if num % 15 == 0  
puts ‘FizzBuzz’  
elsif num % 3 == 0  
puts ‘Fizz’  
elsif num % 5 == 0  
puts ‘Buzz’  
else  
puts num  
end  
end  
end

## if **FILE** == $0 fizzbuzz() end

# fizzbuzz\_test.rb

require ‘spec’  
require ‘fizzbuzz’

describe :fizzbuzz do

# This is just so we can read the results of all those ‘puts’

before do  
@output = StringIO.new()  
$stdout = @output  
fizzbuzz()  
@output.rewind  
@lines = @output.readlines.map {|str| str.chomp}  
end

after do  
$stdout = STDOUT  
end

it “should print 1 as the first line” do  
@lines[0].to\_i.should == 1  
end

it “should print 100 lines” do  
@lines.size.should == 100  
end

it “should print ‘Fizz’ as the third line” do  
@lines[2].should == ‘Fizz’  
end

it “should print ‘Buzz’ as the fifth line” do  
@lines[4].should == ‘Buzz’  
end

it “should print ‘FizzBuzz’ as the fifteenth line” do  
@lines[14].should == ‘FizzBuzz’  
end

it “should print ‘Buzz’ as the last line” do  
@lines.last.should == ‘Buzz’  
end

## end

Now before pulling such a stunt, I might ask plainly if they were  
looking for a trivial solution to a trivial problem, in which case I  
would immediately write down the final answer, or if they wanted me to  
suspend disbelief and imagine this is a part of a large and important  
system rather than a one off game, in which case I might do the  
previous. After all, who wants to work for people that make you guess  
what they want?

Jason M.
