Yet Another Ruby FizzBuzz その4
class Fixnum def fizzbuzz w = {Fizz: 3, Buzz: 5}.select { |_, base| (self%base).zero? } w.empty? ? self : w.keys.join end end (1..100).each { |i| print "#{i.fizzbuzz} " }
Enumerable#injectのほうがいいかな
class Fixnum def fizzbuzz w = {Fizz: 3, Buzz: 5}.inject("") { |mem, (word, base)| (self%base).zero? ? mem << word : mem } w.empty? ? self : w end end (1..100).each { |i| print "#{i.fizzbuzz} " }