4 ms·
Ruby the Ruby way: module FizzBuzz def to_fb s = '' s << (self % 3 == 0 ? 'Fizz' : '') s << (self % 5 == 0 ? 'Buzz' : '') s << se
by qyv 8y 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 }
- smitherfield 8y agoCleaner and more idiomatic version: https://news.ycombinator.com/item?id=18873581#18877403 https://news.ycombinator.com/item?id=18873581#18877403
- qyv 8y agoIMO 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!
- smitherfield 8y agoWell of course. I originally linked to this code: https://news.ycombinator.com/item?id=18873581#18878500 https://news.ycombinator.com/item?id=18873581#18878500 But the edit window had passed by the time I changed it.