简体   繁体   中英

How to loop through an array using .each starting at an index in Ruby

Ok, so we all know that you can do the following:

array.each{ |value|
    puts value
}

What if, however, I want to start at element n instead of starting at the beginning; ie n=0 .

你也可以

array.drop(n).each { |v| puts v }

You can call a subarray with a range like this:

array[n..-1].each { |value| puts value }

Where n is the index you'd like to start at, and -1 will always point to the last index of an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM