Skip to content

Instantly share code, notes, and snippets.

@willf
Created November 2, 2012 15:43
Show Gist options
  • Save willf/4002120 to your computer and use it in GitHub Desktop.
Save willf/4002120 to your computer and use it in GitHub Desktop.
World's best Ruby FizzBuzz
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