Skip to content

Instantly share code, notes, and snippets.

@jacegu
Created December 29, 2010 21:20
Show Gist options
  • Save jacegu/759084 to your computer and use it in GitHub Desktop.
Save jacegu/759084 to your computer and use it in GitHub Desktop.
Another fizzbuzz kata solution with improved readability and OO design
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