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.
str.rstrip
str: This is the string from which we want to remove trailing whitespace characters.
The value returned is a string with any trailing string removed.
# create stringsstr1 = "Edpresso "str2 = "is"str3 = "The\n\t\r"str4 = "\Best !"# remove leading# whitespace charactersputs str1.rstripputs str2.rstripputs str3.rstripputs str4.rstrip
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 strip
method to remove trailing whitespace characters from the strings. Then, we print the result.