Skip to content

Instantly share code, notes, and snippets.

@DavidBechtel
Created January 22, 2013 22:10
Show Gist options
  • Save DavidBechtel/4599015 to your computer and use it in GitHub Desktop.
Save DavidBechtel/4599015 to your computer and use it in GitHub Desktop.
fizzbuzz Ruby style
#!/usr/bin/env ruby -wKU
require 'json'
require 'rspec'
class FizzBuzzGenerator
def initialize(inp)
@number = inp
end
def generate
@output = []
for numb in 1..@number do
@output << "#{numb} fizz" if (numb % 3 == 0 && numb % 5 != 0)
@output << "#{numb} buzz" if (numb % 5 == 0 && numb % 3 != 0)
@output << "#{numb} fizzbuzz" if (numb % 3 == 0 && numb % 5 == 0)
@output << "#{numb}" if (numb % 3 != 0 && numb % 5 != 0)
end
end
def print
@output.each do |line|
puts line
end
end
def html
htm = "<ul>\n"
@output.each do |line|
htm += " <li> #{line} </li> \n"
end
htm = htm += "</ul>"
puts htm
end
def json
@output.to_json.to_json
end
end
doit = FizzBuzzGenerator.new(16)
doit.generate
doit.print
puts doit.html
puts doit.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment