OneBite.Dev - Coding blog in a bite size

reverse arrays items in Ruby

Code snippet on how to reverse arrays items in Ruby

  array.reverse

This code will reverse the order of items in an array. For example, if you have an array of numbers [1,2,3], this code will output [3,2,1]. The reverse method iterates through the array from the last index (array.length - 1) to the first index (0). As it iterates, it swaps the last item with the first item, the second last item with the second item, etc, until each item is placed in the opposite index. Finally, the new array is returned with the reversed order of items.

ruby