Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4616

Get the next item in a .each loop without ending the current item's iteration

$
0
0

I have a collection that I'm cycling through with an .each loop. Is there a way to get the next value without breaking the current iteration for a particular item in the collection?

collection = [foo, bar, quux]collection.each do |item|  # Print the current iteration "foo"  p item #should return foo  # Also print the next iteration "bar"  # without breaking the current each loop for "foo"  p item.something_to_get_barend

Viewing all articles
Browse latest Browse all 4616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>