Skip to content

Instantly share code, notes, and snippets.

@YuukiToriyama
Last active September 12, 2019 18:53
Show Gist options
  • Save YuukiToriyama/5c0ae6381532fd7b539d346dcc8bfd44 to your computer and use it in GitHub Desktop.
Save YuukiToriyama/5c0ae6381532fd7b539d346dcc8bfd44 to your computer and use it in GitHub Desktop.
ruby -e '(1..100).map{|n| case n % 15; when 0 then "FizzBuzz"; when 5,10 then "Buzz"; when 3,6,9,12 then "Fizz"; else n; end}.display'
#!/usr/bin/ruby
# 15で割った余りに注目してFizzBuzz
class Integer
def fizzbuzz
case self % 15
when 0 then "FizzBuzz"
when 5,10 then "Buzz"
when 3,6,9,12 then "Fizz"
else self
end
end
end
(1..100).map(&:fizzbuzz).each{|e| puts e}
@YuukiToriyama
Copy link
Author

関係ないけどScalaで書くとこんなかんじ

(1 to 100) map (n => n % 15 match {case 0 => "FizzBuzz" case 5|10 => "Buzz" case 3|6|9|12 => "Fizz" case _ => n}) foreach println

セミコロンがなくてきれい✨foreach printlnって書き方もいいね💖

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