| > It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Many languages only use `for` to mean a for-each style loop where it reads quite naturally: Python - `for n in range(1,10)` Ruby - `for i in 0..10` Rust - `for n in 1..10` (also picked up the bare `loop`) Others support for-each style loops along with the less than ideal C-style for loop: JavaScript - `for (n of [1,2,3])` (second attempt, for-in has too many gotchas.) Java - `for (int n : {1,2,3})` Otherwise I agree that `while` reads far more naturally. I guess they must have wanted to minimize the number keywords in Go. |
``` (1..10).each do |i| ... end ```
"do" does end up getting used, but only as syntax for a "block" (similar to what other languages would call a lambda).
Ruby also has a shorthand for when you want to do something `n` times without caring about the iteration number:
``` 10.times do ... end ```
One final note is that loop you wrote above is actually different in Ruby than it is for Python and Rust; `x..y` is an inclusive range in Ruby, whereas `range(x, y)` in Python and `x..y` in Rust are exclusive on the right side. I think you might have realized that since you used different starting values for the other two, but you got it backwards; the Python and Rust loops will loop only nine times, whereas the Ruby one will loop 11 times. Ruby does have a syntax for exclusive ranges though; `x...y` is an inclusive range from `x` to `y - 1`. Similarly, in Rust, `x..=y` gives an inclusive range from `x` to `y`. I don't know of any way to do an inclusive range in Python other than manually, but someone with more Python knowledge might be able to provide something for that.