Created
November 26, 2023 11:18
Revisions
-
PragTob revised this gist
Nov 26, 2023 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,6 @@ module FizzBuzz module_function class Rule def initialize(output, applicalbe_divisible_by) @output = output -
PragTob created this gist
Nov 26, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ module FizzBuzz module_function # A rule in the game of FizzBuzz as in, put out this text if it is divisible by x 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