Created
December 18, 2011 10:16
-
-
Save sj26/1492952 to your computer and use it in GitHub Desktop.
I can 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
#!/usr/bin/env ruby -rubygems -ractive_support/all | |
# The one everyone writes | |
(1..100).each do |number| | |
if number % 15 == 0 | |
puts "FizzBuzz" | |
elsif number % 3 == 0 | |
puts "Fizz" | |
elsif number % 5 == 0 | |
puts "Buzz" | |
else | |
puts number | |
end | |
end | |
# The one-line string interpolation approach | |
puts (1..100).map { |number| "#{"Fizz" if number.modulo(3).zero?}#{"Buzz" if number.modulo(5).zero?}".presence or number } | |
# The one-line list compact/concat approach | |
puts (1..100).map { |number| [("Fizz" if number.modulo(3).zero?), ("Buzz" if number.modulo(5).zero?)].compact.join.presence or number } | |
# The one-line array index approach | |
puts (1..100).map { |number| [number, "Fizz", "Buzz", "FizzBuzz"][(number % 3 == 0 ? 1 : 0) + (number % 5 == 0 ? 2 : 0)] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment