Created
December 29, 2010 21:20
-
-
Save jacegu/759084 to your computer and use it in GitHub Desktop.
Another fizzbuzz kata solution with improved readability and OO design
This file contains hidden or 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 FizzBuzzGame | |
FIZZ = 'fizz' | |
BUZZ = 'buzz' | |
FIZZBUZZ = 'fizzbuzz' | |
def answer_for(number) | |
return FIZZBUZZ if number.divisible_by_3? and number.divisible_by_5? | |
return FIZZ if number.divisible_by_3? | |
return BUZZ if number.divisible_by_5? | |
number.value | |
end | |
end | |
class FizzBuzzNumber | |
attr_reader :value | |
def initialize(number) | |
@value= number | |
end | |
def divisible_by_3? | |
divisible_by?(3) | |
end | |
def divisible_by_5? | |
divisible_by?(5) | |
end | |
private | |
def divisible_by?(divisor) | |
@value % divisor == 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment