Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created March 14, 2014 15:10
Show Gist options
  • Save cheeyeo/9549588 to your computer and use it in GitHub Desktop.
Save cheeyeo/9549588 to your computer and use it in GitHub Desktop.
Example of FizzBuzz in Ruby using procs
MULTIPLE_OF_3 = ->(n){ (n % 3).zero? }
MULTIPLE_OF_5 = ->(n){ (n % 5).zero? }
MULTIPLE_OF_3_AND_5 = ->(n){ MULTIPLE_OF_3[n] && MULTIPLE_OF_5[n] }
(1..100).each do |num|
case num
when MULTIPLE_OF_3_AND_5
puts "FizzBuzz"
when MULTIPLE_OF_3
puts "Fizz"
when MULTIPLE_OF_5
puts "Buzz"
else
puts num.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment