News for hackers. Stuff that matters.
Re:Computer History Museum 2018 Fellow: Guido van Rossum by pmontra on Thursday February 22, 2018 @08:41AM (#16437436)

    10.times do
       something
    end

    (1..10).each do |i|
      something_with i
    end
Re:Computer History Museum 2018 Fellow: Guido van Rossum by joshuamorton on Thursday February 22, 2018 @12:09PM (#16439129)

    for _ in range(10):
        something

    for i in range(10):
        something_with(i)
Can you explain why the Ruby reads more naturally?

The major differences, as I see them are that ruby requires end blocks, and uses methods instead of global functions for things.

In my opinion, python takes great pains to make things consistent, which is valuable. There's one function, range, instead of two methods, times and each. The syntax is unified: `for N in range`, which `_` being a convention if `N` is unused, as opposed to Ruby where you can define the looping variable, or not, depending on the method that was called (I guess?).

Ruby does appear to read more like something you might say, although still with weirdish grammar, but on the other hand, replace ":" with "repeat" and be explicit about the elided 0 in range, and you get "for i in the range 0 to 10, repeat something with i" which is english, as opposed to Ruby which is "take the range 1-10 and for each do unto i something with i", which imo appears significantly less natural.

This actually extend's to the stuff about "len" vs ".length", where you're asking which is more natural, to "do something to the length of X" or "do something to X's length". And that's very much a subjective question, but Python's answer establishes stronger consistency.

Re:Computer History Museum 2018 Fellow: Guido van Rossum by pmontra on Sunday March 04, 2018 @02:47PM (#16516259)
IMHO (1..10) is much more explicit than range(10). No questions about what it is. Where does range(10) starts from? -infinity, 0, 1? Or where does it ends at? Is 10 the starting point? Probably not but who knows. One should read the manual for that. BTW I hate Ruby's three dots (1...10) because it's (1..9) and I never remember which one is what when I find it in somebody's else code. Languages should not trick developers into mistakes. I only write the two dots.

However:

1) if one comes from Ruby, then Python is a Ruby designed by someone with a weird mind

2) if one comes from Python, then Ruby is a Python designed by someone with a weird mind

I'm coming from Ruby, a historical accident I can't change. No hard feelings.