Skip to content
instance method

String#rstrip

() -> ::String
rstrip(*selectors) → new_string

Returns a copy of self with trailing whitespace removed; see Whitespace in Strings:

Example
whitespace = "\x00\t\n\v\f\r "
s = whitespace + 'abc' + whitespace
s        # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
s.rstrip # => "\u0000\t\n\v\f\r abc"

If selectors are given, removes characters of selectors from the end of self:

Example
s = "---abc+++"
s.rstrip("+") # => "---abc"

selectors must be valid character selectors (see Character Selectors), and may use any of its valid forms, including negation, ranges, and escapes:

Example
"01234abc56789".rstrip("0-9") # "01234abc"
"01234abc56789".rstrip("0-9", "^4-6") # "01234abc56"

Related: see Converting to New String.

Ruby version changes

  1. 3.0
See also #rstrip! #rpartition #rjust #scan also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0