rubyguides

String#reverse

str.reverse -> string

String#reverse returns a new string with the characters in reverse order. It does not modify the original value, making it safe to use without worrying about unintended side effects. This method is particularly useful for palindrome checking, text processing, and any scenario where you need to flip the character order of a piece of text. The method has been part of Ruby’s standard library since version 1.8 and works consistently across all Ruby implementations including MRI, JRuby, and Rubinius.

The method is small, but it is often the simplest way to express a reverse-order operation on text. It is a natural fit for quick checks, demonstrations, and transformations where you do not want to change the original value in place.

Because it returns a fresh value, reverse is easy to reason about in code that also needs the original value later. That makes it a handy building block in small utility methods, test helpers, and examples where a reader should be able to see the result immediately.

Syntax

string.reverse

Calling reverse on any string flips the character order from end to start and returns the result without touching the original. An empty input simply returns another empty string — there’s nothing to flip, and no error to catch.

Parameters

This method takes no parameters and requires no arguments. The method operates entirely on the string it is called on.

Examples

Basic usage

"hello".reverse
# => "olleh"

"Ruby".reverse
# => "ybuR"

"".reverse
# => ""

The method’s real charm shows with palindromes. Since a palindrome reads the same forward and backward, reverse is the natural way to test for one. The word ‘racecar’ reversed is still ‘racecar’, which is exactly what you’d expect from a method that runs characters from last to first.

Reversing a palindrome

"racecar".reverse
# => "racecar"

"madam".reverse
# => "madam"

When you reverse an entire sentence, the method works character by character, not word by word. That means ‘hello world’ becomes ‘dlrow olleh’ rather than flipping the tokens into ‘world hello.’ If you need to reverse the word order instead, you’ll want split and join — a different kind of reversal that operates on tokens rather than individual characters.

Reversing words in a sentence

"hello world".reverse
# => "dlrow olleh"

Patterns that rely on reverse tend to be compact and self-documenting because the method name says exactly what it does. Palindrome checks, word flipping, and input normalization all benefit from the same simple contract: give me a string and I’ll hand back its mirror image. The next sections collect several of these patterns for quick reference.

common patterns

Checking if a string is a palindrome

def palindrome?(word)
  word == word.reverse
end

palindrome?("racecar")  # => true
palindrome?("hello")   # => false

This pattern keeps the palindrome check direct and readable. The comparison stays obvious because the reversal happens in the same expression that performs the test, so a reader can see the logic without chasing through helper methods or intermediate variables.

Working with multibyte characters

# Works with any string encoding
"Japanese".reverse
# => "esepaJ"

# Handles multibyte characters correctly
"cafe".reverse
# => "efac"

The examples stay simple on purpose: reverse does not care whether the input is short, long, ASCII-only, or already a palindrome. That consistency is what makes it dependable in quick transformations, exercises, and interview problems where you want to focus on the algorithm rather than edge-case handling.

Reversing for coding challenges

# Common interview question: reverse words in a string
def reverse_words(str)
  str.split(" ").reverse.join(" ")
end

reverse_words("hello world")
# => "world hello"

Splitting, reversing, and joining is a common way to reverse word order without reversing the characters inside each word. That gives you a different result from String#reverse, so it is worth keeping the two ideas separate when you choose the method.

Errors

String#reverse never raises an error. It works on empty strings and returns an empty string. The method is completely safe and has no edge cases that would cause exceptions.

That reliability is part of why it is so common in examples and interview questions.

# Reverse pairs well with other string methods
"hello".reverse.upcase    # => "OLLEH"
"hello".reverse.capitalize # => "Olleh"

If you need the original string preserved, reverse is one of the safest string helpers to reach for.

See Also