0

I want to start any where in an array (for example) the last two elements in arr and access the elements as well as their respective indexes 1,2.

Is there a more elegant way to do this?
Without using a the standard method (below) with an index variable i and while.

  arr = [82,217,170]
  i = 3

    while i < arr.length
      puts "#{arr[i]}" + " " + "#{i}"        
    end


Other loop methods can begin at a specific element but return a new array.
I cannot print the elements' respective indexes because its now referring to the new array.

  arr = [82,217,170]

  arr.drop(1).each_with_index do |item,index|
    puts "#{item}" + " " + "#{index}"
  end

  arr[1..2].each_with_index do |item,index|
    puts "#{item}" + " " + "#{index}"
  end 

Output:

217 0 <== should be 1
170 1 <== should be 2
217 0 <== should be 1
170 1 <== should be 2


Reason for question and Uses:

This would be useful if you want to have nested loops in a function that already has a lot of code. You don't have to instantiate multiple index variables outside of each loop.

1
  • This for nested loops sounds like you maybe should use #combination instead... Commented Feb 3, 2018 at 9:15

2 Answers 2

1

If you reverse the positions of your drop and each_with_index calls, you'd have:

arr.each_with_index.drop(1).each { |e, i| puts "#{e} #{i}" }
# 217 1
# 170 2

You could also zip in your indices:

arr.zip(0 .. arr.length-1).drop(1).each { |e, i| puts "#{e} #{i}" }

but there's not much point to that extra complexity. Or you could say this and get right to the point:

(1 .. arr.length-1).each { |i| puts "#{arr[i]} #{i}" }

but that might qualify as a "loop with an index variable".

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

6 Comments

arr.each_with_index does this piece of code return an array?
No, it gives you an Enumerator. But arr.each_with_index.drop(1) leaves you with an Array.
The last solution seems the best for my case. I don't mean to avoid all index variables. I am just avoiding variables instantiated outside of the loop and incremented... such as ones in the case of while loops
Right, thanks. How embarrassing. You could use (1 ... arr.length) (not the three dots) too but I don't see that many three-dot ranges.
mu, I wanted to commend you for using 1 .. arr.length-1 rather than 1 ... arr.length, but from your comment I’m not sure it was intentional. It seems to me that always using two dots will result in fewer bugs than using two here, three there. I’m a two-dot-only coder.
|
1

each_with_index does not take a parameter to specify the starting index; with_index does. Its a method from Enumerator, so let's create one with each.

arr = [82,217,170]
start_at = 1

arr[start_at..-1].each.with_index(start_at){|item,i|puts "#{item} #{i}"}

# 217 1
# 170 2

Comments

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.