Re: [QUIZ] FizzBuzz (#126)

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.