Created
November 26, 2023 11:18
-
-
Save PragTob/537bb657dee52ba30e5a81c7333291f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module FizzBuzz | |
module_function | |
class Rule | |
def initialize(output, applicalbe_divisible_by) | |
@output = output | |
@applicalbe_divisible_by = applicalbe_divisible_by | |
end | |
def apply(number) | |
@output if divisible_by?(number, @applicalbe_divisible_by) | |
end | |
def divisible_by?(number, divisor) | |
number % divisor == 0 | |
end | |
end | |
DEFAULT_RULES = [ | |
Rule.new("Fizz", 3), | |
Rule.new("Buzz", 5) | |
].freeze | |
def fizz_buzz(number, rules = DEFAULT_RULES) | |
applied_rules = rules.filter_map { |rule| rule.apply(number) } | |
if applied_rules.any? | |
applied_rules.join | |
else | |
number | |
end | |
end | |
def run(rules = DEFAULT_RULES) | |
1.upto(100) do |number| | |
puts fizz_buzz(number, rules) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment