Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
String#strip!
(*::String::selector) -> self?
strip!(*selectors) → self or nil
string.c
View on GitHub ↗
static VALUE
rb_str_strip_bang(int argc, VALUE *argv, VALUE str)
{
char *start;
rb_len_t olen, loffset, roffset;
rb_encoding *enc;
str_modify_keep_cr(str);
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);
/* the selector conversion may have modified str */
str_modify_keep_cr(str);
enc = STR_ENC_GET(str);
RSTRING_GETMEM(str, start, olen);
loffset = lstrip_offset_table(str, start, start+olen, enc, table, del, nodel);
roffset = rstrip_offset_table(str, start+loffset, start+olen, enc, table, del, nodel);
}
else {
loffset = lstrip_offset(str, start, start+olen, enc);
roffset = rstrip_offset(str, start+loffset, start+olen, enc);
}
if (loffset > 0 || roffset > 0) {
rb_len_t len = olen-roffset;
if (loffset > 0) {
len -= loffset;
memmove(start, start + loffset, len);
}
STR_SET_LEN(str, len);
TERM_FILL(start+len, rb_enc_mbminlen(enc));
return str;
}
return Qnil;
}
Like String#strip, except that:
-
Any modifications are made to
self. -
Returns
selfif any modification are made,nilotherwise.
Related: see Modifying.