Skip to content

Instantly share code, notes, and snippets.

@omegahm
Last active November 9, 2015 13:53
Show Gist options
  • Save omegahm/c058942e1111f64559d9 to your computer and use it in GitHub Desktop.
Save omegahm/c058942e1111f64559d9 to your computer and use it in GitHub Desktop.
Fizzbuzz in Ruby
class Fiznum
attr_accessor :num
def initialize(num)
self.num = num
end
def fizzbuzz?
num % 15 == 0
end
def fizz?
num % 3 == 0
end
def buzz?
num % 5 == 0
end
def to_s
case
when fizzbuzz?
"FizzBuzz"
when fizz?
"Fizz"
when buzz?
"Buzz"
else
num.to_s
end
end
end
puts (1..100).map { |n| Fiznum.new(n) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment