Skip to content

Instantly share code, notes, and snippets.

@keccers
Created August 1, 2013 03:35
Show Gist options
  • Save keccers/6128219 to your computer and use it in GitHub Desktop.
Save keccers/6128219 to your computer and use it in GitHub Desktop.
OO Ruby FizzBuzz
class FizzBuzz
attr_reader :limit, :sequence
def initialize(limit)
@limit = limit
@sequence = []
end
def display
generate_sequence
sequence.join(" ")
end
private
def generate_sequence
(1..limit).to_a.each do |i|
sequence << fizz_or_buzz(i)
end
end
def fizz_or_buzz(number)
literal = ""
literal << "Fizz" if number % 3 == 0
literal << "Buzz" if number % 5 == 0
literal = number if literal.length == 0
literal
end
end
#DRIVER
fb = FizzBuzz.new(100)
puts fb.display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment