String#reverse
str.reverse -> string String · Updated March 13, 2026 · String Methods String#reverse returns a new string with the characters in reverse order. It does not modify the original string, 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 string. 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.
Syntax
string.reverse
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
# => ""
Reversing a palindrome
"racecar".reverse
# => "racecar"
"madam".reverse
# => "madam"
Reversing words in a sentence
"hello world".reverse
# => "dlrow olleh"
Common Patterns
Checking if a string is a palindrome
def palindrome?(word)
word == word.reverse
end
palindrome?("racecar") # => true
palindrome?("hello") # => false
Working with multibyte characters
# Works with any string encoding
"Japanese".reverse
# => "esepaJ"
# Handles multibyte characters correctly
"cafe".reverse
# => "efac"
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"
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.