4 ms·
> Ruby allows for nicer looking code and readability Have you ever actually used Ruby? Here's what FizzBuzz looks like in Python: def fizz_buzz(number):
by csense 11y ago
> Ruby allows for nicer looking code and readability
Have you ever actually used Ruby?
Here's what FizzBuzz looks like in Python:
def fizz_buzz(number):
if number%3 == 0:
return "fizz"
if number%5 == 0:
return "buzz"
return str(number)
for num in range(1,101):
print(fizz_buzz(num))
Compare that to the hideous mess of fizz buzz in Ruby [1] -- there's a lot of magic going on there. Many unfamiliar operators, line #2 is a magic string interpolation thing with tons of weird escaping going on (the #{} seems to be some kind of template which makes stuff inside the "" string actually be interpreted as code which itself contains '' strings, and I don't want to think about the potential injection vectors that could be introduced by this), the ternary operator, absolute value bars, an anonymous function, and defining a function by binding an anonymous function created with -> function call syntax to a name.
People on HN keep saying Ruby is intuitive and easy to learn -- no it isn't. The Python code by contrast is basically the same as the pseudo code you would write on a napkin in "not-any-particular-language-I-just-want-to-describe-an-algorithm-without-language-specific-issues-to-get-in-my-way".
[1] http://hostiledeveloper.com/2014/04/26/on-why-learning-to-program-is-hard.html http://hostiledeveloper.com/2014/04/26/on-why-learning-to-pr...
- zimpenfish 11y agoHere's what your code looks like in Ruby: def fizz_buzz(number) if number%3 == 0 then return "fizz" end if number%5 == 0 then return "buzz" end return number end for num in 1.upto(100) do puts(fizz_buzz(num)) end Don't mistake someone fannying about with code-golfing for the language itself. (Also your code is wrong - it never prints "fizzbuzz" which it should for 15, 30, 45, etc.)