Skip to content

Instantly share code, notes, and snippets.

@rjollet
Last active September 23, 2016 14:27
Show Gist options
  • Save rjollet/77af9e160a4dc51f97b72af31f49f586 to your computer and use it in GitHub Desktop.
Save rjollet/77af9e160a4dc51f97b72af31f49f586 to your computer and use it in GitHub Desktop.
fizzbuzz
def fizzbuzz(size, &output)
(1..size).map do |number|
res = ''
res << 'Fizz' if (number % 3).zero?
res << 'Buzz' if (number % 5).zero?
res = res == '' ? number : res
yield res if output
res
end
end
require 'minitest/autorun'
require 'minitest/rg'
require_relative 'fizzbuzz.rb'
describe 'fizzbuzz should succed the test' do
it 'fizzbuzz should handle fizz and buzz' do
fizzbuzz(5).must_equal [1, 2, 'Fizz', 4, 'Buzz']
end
it 'fizzbuzz should handle fizz buzz and fizzbuzz' do
fizzbuzz(20).must_equal [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz']
end
it 'fizzbuzz should handle yield to select some specific items' do
fizzbuzz(5).select { |item| item.is_a? Integer }.must_equal [1, 2, 4]
end
it 'ffizzbuzz should handle yield to concate items' do
$stdout = StringIO.new
fizzbuzz(20) { |item| print "#{item}-" }
$stdout.string.must_equal '1-2-Fizz-4-Buzz-Fizz-7-8-Fizz-Buzz-11-Fizz-13-14-FizzBuzz-16-17-Fizz-19-Buzz-'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment