Skip to content

Instantly share code, notes, and snippets.

@triangletodd
Last active December 12, 2020 22:43
Show Gist options
  • Save triangletodd/65d21f7c9ed686d59cf5 to your computer and use it in GitHub Desktop.
Save triangletodd/65d21f7c9ed686d59cf5 to your computer and use it in GitHub Desktop.
Ruby Fizzbuzz
#!/usr/bin/env ruby
# Requires Ruby 2.1
# BUT.. Keeps the code clean without monkey patching
module Refinements
refine Integer do
def div_by?(int)
self % int == 0
end
end
end
class Fizzbuzz
using Refinements
def self.[](int)
return 'FizzBuzz' if int.div_by?(3) && int.div_by?(5)
return 'Fizz' if int.div_by?(3)
return 'Buzz' if int.div_by?(5)
int
end
def initialize(integers)
@items = integers.map{ |int| Fizzbuzz[int] }
end
def to_a
@items
end
def print
@items.each { |item| puts item }
end
end
Fizzbuzz.new(1..100).print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment