Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
I can FizzBuzz
#!/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