Skip to content

Instantly share code, notes, and snippets.

@RomanTurner
Created June 27, 2021 00:59
Show Gist options
  • Save RomanTurner/c9d84500491bb3ef48e9004d75a794f8 to your computer and use it in GitHub Desktop.
Save RomanTurner/c9d84500491bb3ef48e9004d75a794f8 to your computer and use it in GitHub Desktop.
Fizzbuzz oop ruby
# Here is how I would implement Fizzbuzz with objects
# The main issue with your class is what's called
# single responsibility principle
# classes should do one thing and be about
# instances of those classes
# the fizzbuzz class should be about the loop
# just solving fizzbuzz individually
class Fizzbuzz
attr_accessor :value
def initialize(value)
@value = value
end
def fizz?
value % 3 == 0
end
def buzz?
value % 5 == 0
end
def fizzbuzz?
fizz? && buzz?
end
def answer
if fizzbuzz?
"fizzbuzz"
elsif buzz?
"buzz"
elsif fizz?
"fizz"
end
end
end
(1..50).each do |i|
puts "#{i} is #{Fizzbuzz.new(i).answer}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment