To reverse a string in Ruby, you can use the reverse
method.
In this example,
str1
with a value of "Hello World"
.reverse
method on str1
.str1 = "Hello World"
reversed_str1 = str1.reverse
puts "Reversed string is: #{reversed_str1}"
Reversed string is: dlroW olleH
In this example,
str2
with a value of "apple"
.reverse
method on str2
.str2 = "apple"
reversed_str2 = str2.reverse
puts "Reversed string is: #{reversed_str2}"
Reversed string is: elppa
In this tutorial, we learned How to Reverse a String in Ruby language with well detailed examples.