Skip to content

Instantly share code, notes, and snippets.

@DavidBechtel
Created January 24, 2013 21:11
Show Gist options
  • Save DavidBechtel/4627788 to your computer and use it in GitHub Desktop.
Save DavidBechtel/4627788 to your computer and use it in GitHub Desktop.
fizzbuzz generator creates an array with the input value as the upper limit
# FizzBuzz_Generator.rb
require 'json'
require 'rspec'
class FizzBuzzGenerator < Array
def initialize(high_value = 0)
@highest_value = high_value.to_i
# Error test
self << "Zero, negative numbers, strings that describe a number, and nil not supported" if (@highest_value <= 0)
# if not error, then create the array contents
if @highest_value > 0 then
for test_number in 1..@highest_value do
self << "#{test_number} fizz " if (test_number % 3 == 0 && test_number % 5 != 0)
self << "#{test_number} buzz " if (test_number % 5 == 0 && test_number % 3 != 0)
self << "#{test_number} fizzbuzz " if (test_number % 15 == 0)
self << "#{test_number} " if (test_number % 3 != 0 && test_number % 5 != 0)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment