Created
January 23, 2013 16:41
-
-
Save DavidBechtel/4609485 to your computer and use it in GitHub Desktop.
fizzbuzz_generator Class based with print, html and json output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FizzBuzz_Generator.rb | |
require 'json' | |
require 'rspec' | |
class FizzBuzzGenerator | |
def initialize(high_value = 0) | |
@highest_value = high_value.to_i | |
end | |
def generate | |
@output_array = [] | |
@output_array << "Zero, negative numbers, strings that describe a number, and nil not supported" if (@highest_value <= 0) | |
if @highest_value > 0 then | |
for test_number in 1..@highest_value do | |
@output_array << "#{test_number} fizz " if (test_number % 3 == 0 && test_number % 5 != 0) | |
@output_array << "#{test_number} buzz " if (test_number % 5 == 0 && test_number % 3 != 0) | |
@output_array << "#{test_number} fizzbuzz " if (test_number % 15 == 0) | |
@output_array << "#{test_number} " if (test_number % 3 != 0 && test_number % 5 != 0) | |
end | |
end | |
end | |
def print | |
@output_array.each do |line| | |
puts line | |
end | |
end | |
def html | |
htm = "<ul>\n" | |
@output_array.each do |line| | |
htm += " <li> #{line} </li> \n" | |
end | |
htm = htm += "</ul>" | |
return htm | |
end | |
def json | |
@output_array.to_json.to_json | |
end | |
end | |
#doit = FizzBuzzGenerator.new("8") | |
#doit.generate | |
#doit.print | |
#doit.html | |
#puts doit.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're doing great! Keep working at it!
Some suggestions...
Try eliminating the output_array instance variable and having the generate method return the array instead. Print, html and json would call generate instead of referencing the variable, and your client won't need to call generate at all. It's a good idea to hide as much of your implementation as you can manage.
Client usage goes from this:
to this:
It's a bit less code on the client side, but now if you choose to rename or even eliminate the generate method, the client's code will still work.
Extracting the actual calculation of fizz/buzz/fizzbuzz for each number into its own method and calling it from inside generate would reduce the generate method's responsibility to one thing (adding values to an array) and delegate the calculation (which can then be tested directly against individual numbers for correctness). If the calculation method is called "fizzbuzzified" then that part of generate would look like this:
(I switched from the "for" to using "each", which is more idiomatic Ruby)
Also, the actual fizzbuzz logic cleans itself up a lot if you check for test_number % 15 first...
And unfortunately since the challenge says "output fizz instead", etc, the actual output is incorrect. Your implementation does "output fizz as well", which is not the same thing.
Hope that's useful!