Skip to content

Instantly share code, notes, and snippets.

@bchase
Last active August 29, 2015 14:19
Show Gist options
  • Save bchase/4d829ee3b3af70116402 to your computer and use it in GitHub Desktop.
Save bchase/4d829ee3b3af70116402 to your computer and use it in GitHub Desktop.
# first solution
(1..100).map{|n|s="#{'Fizz'if n%3==0}#{'Buzz'if n%5==0}";p s.empty?? n:s}
# second implemenation
(1..100).map{|n|p %W[#{n} Fizz Buzz FizzBuzz][n%15==0?3:n%3==0?1:n%5==0?2:0]}
(1..100).map{|n|p %w[Fizz Buzz FizzBuzz][n%15==0?2:n%3==0?0:n%5==0?1:7]||n}
# combined and shortened
(1..100).map{|n|p "#{n%3==0?n%5==0?'FizzBuzz':'Fizz':n%5==0?'Buzz':n}"}
(1..100).map{|n|p n%3==0?n%5==0?'FizzBuzz':'Fizz':n%5==0?'Buzz':n}
# second impl w/ shorter ternary ops
(1..100).map{|n|p %w[Fizz Buzz FizzBuzz][n%3==0?n%5==0?2:0:n%5==0?1:7]||n}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment