Skip to content

Instantly share code, notes, and snippets.

@sasaki-shigeo
Last active December 20, 2015 04:29
Show Gist options
  • Save sasaki-shigeo/6071411 to your computer and use it in GitHub Desktop.
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)
#
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
@sasaki-shigeo
Copy link
Author

Fizz Buzz in two ways
(1) for and if; and
(2) map, case and lambda block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment