Last active
December 12, 2020 22:43
-
-
Save triangletodd/65d21f7c9ed686d59cf5 to your computer and use it in GitHub Desktop.
Ruby Fizzbuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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