Hacker News new | ask | show | jobs
by qyv 2724 days ago
Ruby the Ruby way:

  module FizzBuzz
    def to_fb
      s = ''
      s << (self % 3 == 0 ? 'Fizz' : '')
      s << (self % 5 == 0 ? 'Buzz' : '')
      s << self.to_s if s.empty?
      return s
    end
  end

  Integer.include(FizzBuzz)

  1.upto(100) { |i| puts i.to_fb }
1 comments

Cleaner and more idiomatic version: https://news.ycombinator.com/item?id=18873581#18877403
IMO monkey-patching built-in classes is idiomatic Ruby!

Edit to add: Actually, now that I think about it, using method_missing would have been even better!

Well of course. I originally linked to this code: https://news.ycombinator.com/item?id=18873581#18878500

But the edit window had passed by the time I changed it.