Last active
August 29, 2015 14:27
-
-
Save kangkyu/14eb89318369bf375efe to your computer and use it in GitHub Desktop.
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
def fizz(number) | |
number % 3 == 0 ? "fizz" : "" | |
end | |
def buzz(number) | |
number % 5 == 0 ? "buzz" : "" | |
end | |
def fizz_buzz(number) | |
(number.to_s + " " + fizz(number) + buzz(number)) | |
.chomp(" ") | |
end | |
def run_fizz_buzz(limit) | |
(1..limit).each do |number| | |
puts fizz_buzz(number) | |
end | |
end | |
run_fizz_buzz(100) |
Thank you @charliemcelfresh for your comment. I also made an additional change, I pulled out fizz
and buzz
as separate methods.
def fizz_buzz(number)
( number.to_s +
" " +
(number % 3 == 0 ? "fizz" : "") +
(number % 5 == 0 ? "buzz" : "")
).chomp(" ")
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kang, I like this solution a ton. It is very good Ruby to put all your logic in one line as you did. Couple comments:
-- I know you see a lot of people call methods without parens, but in my opinion, that is not proper (are you looking at https://github.com/styleguide/ruby? Not sure this is mentioned there explicitly, but I think it is implicitly)
-- not crazy about that space after the counter, if there is no 'fizz', 'buzz', or 'fizzbuzz'
-- again, though, I like your solution a lot !