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 strings
str1 = "Edpresso "
str2 = "is"
str3 = "The\n\t\r"
str4 = "\Best !"
# remove leading
# whitespace characters
puts str1.rstrip
puts str2.rstrip
puts str3.rstrip
puts 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 strip method to remove trailing whitespace characters from the strings. Then, we print the result.

Free Resources