Created
November 16, 2012 00:56
-
-
Save alandotcom/4082913 to your computer and use it in GitHub Desktop.
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
=begin | |
Write a program that prints the numbers from 1 to 100. | |
But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". | |
For numbers which are multiples of both three and five print "FizzBuzz". | |
(http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html) | |
=end | |
module FizzBuzz | |
def self.fizzbuzz(n) | |
case | |
when (n % 15).zero? then return "FizzBuzz" | |
when (n % 3).zero? then return "Fizz" | |
when (n % 5).zero? then return "Buzz" | |
else return n | |
end | |
end | |
def self.getfizzbuzz!(n) | |
1.upto(n).reduce([]) { |memo, n| memo.push(self.fizzbuzz(n)) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment