collection.map do |i|
blah = something(i.something)
blah.something_else
end
2) Easily chainable object methods instead of weird global functions: a.length
collection.map { |x| x^2 }
vs len(a)
map(lambda x: x^2, collection)
3) Closing braces instead of whitespace make it a lot easier to judge indentation levelWhen you're working in a large project with +10K LOC, the project structure and the quality of the code you write is much more important than the language itself.
10.times do
something
end
(1..10).each do |i|
something_with i
end 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.
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.
I'm an amateur programmer (Python, JS) and I have been wondering for a long time if there is a language more readable than python.