instance method
String#rstrip
() -> ::String
rstrip(*selectors) → new_string
string.c
View on GitHub ↗
static VALUE
rb_str_rstrip(int argc, VALUE *argv, VALUE str)
{
rb_encoding *enc;
char *start;
long olen, roffset;
enc = STR_ENC_GET(str);
RSTRING_GETMEM(str, start, olen);
if (argc > 0) {
char table[TR_TABLE_SIZE];
VALUE del = 0, nodel = 0;
tr_setup_table_multi(table, &del, &nodel, str, argc, argv);
roffset = rstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
}
else {
roffset = rstrip_offset(str, start, start+olen, enc);
}
if (roffset <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, 0, olen-roffset);
}
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
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-