Last active
November 9, 2015 13:53
-
-
Save omegahm/c058942e1111f64559d9 to your computer and use it in GitHub Desktop.
Fizzbuzz in Ruby
This file contains 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
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