How can I iterate over an array starting at a specific index?

I want to start iterating over an array from a specific index. How can i do this?

myj.each do |temp| 
###
end

      

+3


source to share


3 answers


Follow these steps:



your_array[your_index..-1].each do |temp| 
  ###
end

      

+7


source


It would be more idiomatic to use Enumerable#drop

:



myj.drop(index).each do |temp| 
  ###
end

      

+7


source


You can use a simple loop. Assuming array is arr, start index is "start" and end index is "end"

for i in start..end do
  #Access array items by arr[i]
end

      

0


source







All Articles