Last active
July 2, 2016 12:18
-
-
Save wittawasw/a6de2ac2cf7bbff1d234876c7a27023b to your computer and use it in GitHub Desktop.
fizzbuzz module in Ruby
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 characters
module FizzBuzz | |
class << self | |
def out(range: 1..100) | |
range.map { |num| print(num) } | |
end | |
private | |
def print(i) | |
if fizzbuzz?(i) | |
'fizzbuzz' | |
elsif fizz?(i) | |
'fizz' | |
elsif buzz?(i) | |
'buzz' | |
else | |
i | |
end | |
end | |
def fizzbuzz?(i) | |
fizz?(i) && buzz?(i) | |
end | |
def fizz?(i) | |
i % 3 == 0 | |
end | |
def buzz?(i) | |
i % 5 == 0 | |
end | |
end | |
end | |
# puts FizzBuzz.out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment