Created
August 1, 2013 03:35
-
-
Save keccers/6128219 to your computer and use it in GitHub Desktop.
OO Ruby FizzBuzz
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
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