Ruby Program to Reverse a String with Examples and Explanation
You can reverse a string in Ruby using the
reverse method like this: reversed = original_string.reverse.Examples
Inputhello
Outputolleh
InputRuby
OutputybuR
Input
Output
How to Think About It
To reverse a string, think of reading the characters from the end to the start. You take the last character first, then the second last, and so on until you reach the first character. This creates a new string with characters in reverse order.
Algorithm
1
Get the input string.2
Start from the last character of the string.3
Collect characters moving backward to the first character.4
Form a new string from these collected characters.5
Return or print the new reversed string.Code
ruby
puts "Enter a string:" input = gets.chomp reversed = input.reverse puts "Reversed string: #{reversed}"
Output
Enter a string:
hello
Reversed string: olleh
Dry Run
Let's trace the input 'hello' through the code
1
Input received
input = 'hello'
2
Reverse method called
reversed = 'hello'.reverse
3
Reverse method result
reversed = 'olleh'
4
Output printed
Print 'Reversed string: olleh'
| Step | Input | Output |
|---|---|---|
| 1 | hello | hello |
| 2 | hello | olleh |
| 3 | olleh | olleh |
Why This Works
Step 1: Using the reverse method
The reverse method in Ruby returns a new string with characters in the opposite order.
Step 2: No manual looping needed
Ruby handles the character order internally, so you don't need to write loops to reverse the string.
Step 3: Output the reversed string
We print the reversed string using puts to show the result to the user.
Alternative Approaches
Manual loop with each_char
ruby
puts "Enter a string:" input = gets.chomp reversed = "" input.each_char { |char| reversed = char + reversed } puts "Reversed string: #{reversed}"
This method builds the reversed string by adding each character to the front, which is more manual but shows the process clearly.
Using array reverse
ruby
puts "Enter a string:" input = gets.chomp reversed = input.chars.reverse.join puts "Reversed string: #{reversed}"
This converts the string to an array of characters, reverses the array, then joins it back to a string. Slightly longer but useful to understand arrays.
Complexity: O(n) time, O(n) space
Time Complexity
The reverse method processes each character once, so it takes time proportional to the string length, O(n).
Space Complexity
It creates a new string to hold the reversed characters, so space used is also O(n).
Which Approach is Fastest?
Using the built-in reverse method is fastest and most readable compared to manual loops or array conversions.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Built-in reverse method | O(n) | O(n) | Simple and fast string reversal |
| Manual loop with each_char | O(n) | O(n) | Learning how reversal works step-by-step |
| Array reverse and join | O(n) | O(n) | When working with arrays and strings together |
Use Ruby's built-in
reverse method for the simplest and fastest way to reverse strings.Beginners often try to reverse strings by looping forward instead of backward, which does not reverse the string.