Let’s say you’re tasked with writing a Ruby application that takes an array and creates a new array out of every other element from the original array. It might be tempting to use the each_with_index method available to arrays to loop through the original array as follows:
|||new_array = |
|||array.each_with_index do |element, index||
|||new_array << element if index.even?|
In this implementation, we iterate through each element of the original array and only add the element to the new array if the index of that element is an even number.
When analyzing the steps taking place here, we can see that there are really two types of steps. We have one type of ...