0
0
RubyProgramBeginner · 2 min read

Ruby Program to Reverse Array with Example and Explanation

In Ruby, you can reverse an array using the reverse method like this: reversed_array = array.reverse.
📋

Examples

Input[1, 2, 3]
Output[3, 2, 1]
Input["apple", "banana", "cherry"]
Output["cherry", "banana", "apple"]
Input[]
Output[]
🧠

How to Think About It

To reverse an array, think about flipping the order of elements so the last becomes first and the first becomes last. Ruby provides a simple method reverse that does this for you without needing to manually swap elements.
📐

Algorithm

1
Get the input array.
2
Use the built-in <code>reverse</code> method to create a new array with elements in reverse order.
3
Return or print the reversed array.
💻

Code

ruby
array = [1, 2, 3, 4, 5]
reversed_array = array.reverse
puts reversed_array.inspect
Output
[5, 4, 3, 2, 1]
🔍

Dry Run

Let's trace the array [1, 2, 3, 4, 5] through the code

1

Original array

array = [1, 2, 3, 4, 5]

2

Reverse the array

reversed_array = array.reverse # reversed_array = [5, 4, 3, 2, 1]

3

Print the reversed array

puts reversed_array.inspect # outputs [5, 4, 3, 2, 1]

StepArray State
Initial[1, 2, 3, 4, 5]
After reverse[5, 4, 3, 2, 1]
💡

Why This Works

Step 1: Using the reverse method

The reverse method returns a new array with elements in the opposite order without changing the original array.

Step 2: Storing the reversed array

We save the reversed array in a new variable to keep the original array intact.

Step 3: Printing the result

Using puts with inspect shows the array in a readable format.

🔄

Alternative Approaches

reverse! method
ruby
array = [1, 2, 3, 4, 5]
array.reverse!
puts array.inspect
This method reverses the array in place, changing the original array instead of creating a new one.
Manual swapping
ruby
array = [1, 2, 3, 4, 5]
left = 0
right = array.length - 1
while left < right
  array[left], array[right] = array[right], array[left]
  left += 1
  right -= 1
end
puts array.inspect
This approach manually swaps elements from both ends moving toward the center, useful for understanding the process but more complex.

Complexity: O(n) time, O(n) space

Time Complexity

The reverse method processes each element once, so it takes linear time proportional to the array size.

Space Complexity

It creates a new array to hold the reversed elements, so it uses extra space proportional to the array size.

Which Approach is Fastest?

Using reverse! is faster in space because it reverses in place, but reverse is safer when you want to keep the original array unchanged.

ApproachTimeSpaceBest For
reverseO(n)O(n)When you want a reversed copy without changing original
reverse!O(n)O(1)When you want to reverse the array in place to save memory
Manual swappingO(n)O(1)Learning how reversal works internally or custom logic
💡
Use reverse for a simple way to get a reversed copy of an array without modifying the original.
⚠️
Beginners often try to reverse the array by looping incorrectly or forget that reverse returns a new array and does not change the original.