Created
October 31, 2011 00:16
-
-
Save jeffreyiacono/1326634 to your computer and use it in GitHub Desktop.
FizzBuzz
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
# written after reading: | |
# http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html | |
# moderator class so we can encapsulate everything and if we need to change the | |
# moderation (#moderate) algo, we can easily do that without messing with the rest | |
# of the program | |
class Moderator | |
# moderate algo: | |
# if number is divisible by 3 and 5, return "FizzBuzz" | |
# else if number is divisible by only 3, return "Fizz" | |
# else if number is divisible by only 5, return "Buzz" | |
# otherwise just return the number | |
def moderate(i) | |
if i % 3 == 0 && i % 5 == 0 | |
return "FizzBuzz" | |
elsif i % 3 == 0 | |
return "Fizz" | |
elsif i % 5 == 0 | |
return "Buzz" | |
else | |
return i | |
end | |
end | |
end | |
# can I get a moderator up in here? | |
moderator = Moderator.new | |
# moderate the range 1 - 100, inclusive and print the moderated result | |
(1..100).each do |i| | |
puts moderator.moderate(i) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment