In Ruby, we use the
reverse! method to permanently reverse a string.
reverse!method is different from the
reversedoes not permanently modify a string, but
When we call the
reverse! method on a string, the original string is modified and reversed. This means the characters in the string are reversed.
str: The string we want to permanently reverse.
This method returns a new string that is the reverse of the original.
# create strings str1 = "this" str2 = "is" str3 = "not" str4 = "reversed" # reverse strings # permanently str1.reverse! str2.reverse! str3.reverse! str4.reverse! # print strings puts str1 puts str2 puts str3 puts str4
Lines 2 to 5: We create some strings.
Line 9 to 12: We call the
reverse method on the strings to permanently reverse the strings.
Line 15 to 18: We print the permanently reversed strings to the console.
View all Courses