Skip to content

Instantly share code, notes, and snippets.

@alandotcom
Created November 16, 2012 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandotcom/4082913 to your computer and use it in GitHub Desktop.
Save alandotcom/4082913 to your computer and use it in GitHub Desktop.
fizzbuzz
=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
@nateberkopec
Copy link

(1..100).map{|i|(f=[["Fizz"][i%3],["Buzz"][i%5]].join).empty? ? i:f}

@alandotcom
Copy link
Author

That is second to the solution that only uses lambdas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment