1

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; i.e. n=0.

2 Answers 2

2

You can also do

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is drop mutative; that is, if I call upon it again will the 0...n-1 entries still be there?
drop doesn't modify the original array
1

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.

1 Comment

...or insert next if value < n, which avoids the need to create the temporary array array[n..-1]. You could also write puts array[n..-1].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.