Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Last active August 29, 2015 14:27
Show Gist options
  • Save kangkyu/14eb89318369bf375efe to your computer and use it in GitHub Desktop.
Save kangkyu/14eb89318369bf375efe to your computer and use it in GitHub Desktop.
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)
@charliemcelfresh
Copy link

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 !

@kangkyu
Copy link
Author

kangkyu commented Aug 13, 2015

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