Created
November 2, 2012 15:43
-
-
Save willf/4002120 to your computer and use it in GitHub Desktop.
World's best 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 | |
private | |
@@pairs = [ | |
[3, "fizz"], | |
[5, "buzz"] | |
] | |
def self.strings_for(n) | |
@@pairs.map{|k,v| n % k == 0 ? v : nil}.compact | |
end | |
def self.joins(strings,n) | |
(strings.size == 0 ? [n.to_s] : strings).join("") | |
end | |
public | |
def self.stringify(n) | |
self.joins(self.strings_for(n),n) | |
end | |
end | |
def main | |
1.upto(100).each{|i| puts FizzBuzz.stringify(i)}; true | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment