What is the Ruby rstrip method of string?
Overview
In Ruby, rstrip, or right strip, removes a trailing whitespace character in a string. This whitespace character could be a space, null, horizontal, or vertical tab, line feed, form feed, carriage return, etc.
Syntax
str.rstrip
Parameters
str: This is the string from which we want to remove trailing whitespace characters.
Return value
The value returned is a string with any trailing string removed.
Code example
# create stringsstr1 = "Edpresso "str2 = "is"str3 = "The\n\t\r"str4 = "\Best !"# remove leading# whitespace charactersputs str1.rstripputs str2.rstripputs str3.rstripputs str4.rstrip
Explanation
-
Line 2 to line 5: We create some strings from which we want to remove trailing whitespace characters.
-
Line 9 to line 12: We use the
stripmethod to remove trailing whitespace characters from the strings. Then, we print the result.