Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fizz Buzz Solver
require 'singleton'
class FizzBuzzSolver
include Singleton
FIZZ = 3
BUZZ = 5
def run(numbers)
answer = solve(numbers)
answer.join("\n")
end
private
def solve(numbers)
numbers.map do |num|
next 'fizzbuzz' if fizz?(num) && buzz?(num)
next 'fizz' if fizz?(num)
next 'buzz' if buzz?(num)
next num
end
end
def fizz?(num)
(num % FIZZ).zero?
end
def buzz?(num)
(num % BUZZ).zero?
end
end
puts FizzBuzzSolver.instance.run((1..20).to_a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment