Looping differences in Ruby using Range vs. Times

This information can be easily obtained by inspecting the documentation.

Array#each has a signature of array.each {|item| block } → array, so we can see that the return value of foo.each { ... } is foo.

Likewise, Int#upto has a signature of int.upto(limit) {|i| block } => int, so that x.upto(y) { ... } will always return x.

Then we can also see that 2.times { ... } will return 2 because of Integer#times‘s signature.

I’m having trouble finding the right documentation, but for x in y... gets translated to y.each do |x| ..., so that’s why your for-in loop returns the same thing as your .each loop.

Anyway, depending on the return values of these looping constructs is… a strange approach. I don’t think this happens much (at all?) in idiomatic Ruby code.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)