Last active
December 20, 2015 04:29
-
-
Save sasaki-shigeo/6071411 to your computer and use it in GitHub Desktop.
Fizz Buzz in Ruby (example code on if/case and for/iterators)
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
# | |
for n in 1..100 | |
if n % 15 == 0 | |
puts 'fizzbuzz' | |
elsif n % 3 == 0 | |
puts 'fizz' | |
elsif n % 5 == 0 | |
puts 'buzz' | |
else | |
puts n | |
end | |
end | |
fb = lambda {|n| | |
case n % 15 | |
when 0 then | |
'fizzbuzz' | |
when 3, 6, 9, 12 | |
'fizz' | |
when 5, 10 | |
'buzz' | |
else | |
n | |
end | |
} | |
(1..100).map &fb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fizz Buzz in two ways
(1) for and if; and
(2) map, case and lambda block