search
LoginSignup
26

More than 5 years have passed since last update.

posted at

updated at

♥Enumerator を使ったシンプルな FizzBuzz♥

Enumerator を使ったシンプルな FizzBuzz です。

fizzer = [nil, nil, 'Fizz'].cycle
buzzer = [nil, nil, nil, nil, 'Buzz'].cycle
1.upto(100) { |i|
  fizz, buzz = fizzer.next, buzzer.next
  puts fizz || buzz ? "#{fizz}#{buzz}" : i
}

Array#cycle がクールですね♥

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
26