Created
August 8, 2012 04:38
-
-
Save parrot-studio/3292084 to your computer and use it in GitHub Desktop.
FizzBuzzを書いたことがないとまずいらしいので・・・
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
# coding: utf-8 | |
(1..100).each do |n| | |
puts case | |
when n % 15 == 0 | |
'FizzBuzz' | |
when n % 3 == 0 | |
'Fizz' | |
when n % 5 == 0 | |
'Buzz' | |
else | |
n | |
end | |
end |
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
# coding: utf-8 | |
(1..100).each do |n| | |
fb = '' | |
fb << 'Fizz' if n % 3 == 0 | |
fb << 'Buzz' if n % 5 == 0 | |
puts (fb.empty? ? n : fb) | |
end |
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
# coding: utf-8 | |
class Numeric | |
def divided_by?(n) | |
self.modulo(n).zero? | |
end | |
end | |
def fizzbuzz(n) | |
fb = '' | |
fb << 'Fizz' if n.divided_by?(3) | |
fb << 'Buzz' if n.divided_by?(5) | |
fb.empty? ? n : fb | |
end | |
(1..100).each{|n| puts fizzbuzz(n)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment