I have two solutions. One is the quick job interview version:
(1..100).each { |x| str = ''; str += 'Fizz' if (x % 3).zero?; str +=
'Buzz' if (x % 5).zero?; str = x if str.empty?; puts str }
The second is inspired by Peter Seebach's early suggestion for "extra
fun". It's not much different from the above, but it puts the logic
in Fixnum:
class Fixnum
def to_s
str = ''
str += 'Fizz' if (self % 3).zero?
str += 'Buzz' if (self % 5).zero?
str = '%d' % self if str.empty?
str
end
end
(1..100).each { |x| puts x }
My favorite thing about that solution is seeing irb prompts like
"irb(main):013:FizzBuzz>" and seeing that the range is output as
"1..Buzz"