红宝石 |字符串反转方法
reverse是 Ruby 中的 String 类方法,用于返回一个新字符串,其中包含给定字符串中的字符以相反的顺序排列。
Syntax: str.reverse
Parameters: Here, str is the string which is to be reversed.
Returns: This method returns a new string in reversed order.
示例 1:
# Ruby program to demonstrate
# the reverse method
# Taking a string and
# using the method
puts "GeeksforGeeks".reverse
puts "Ruby".reverse
输出:
skeeGrofskeeG
ybuR
示例 2:
# Ruby program to demonstrate
# the reverse method
# Taking a string and
# using the method
puts "String".reverse
puts "Class".reverse
输出:
gnirtS
ssalC