Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
String#chop!
() -> self?
chop! → self or nil
string.c
View on GitHub ↗
static VALUE
rb_str_chop_bang(VALUE str)
{
str_modify_keep_cr(str);
if (RSTRING_LEN(str) > 0) {
rb_len_t len;
len = chopped_length(str);
STR_SET_LEN(str, len);
TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
if (ENC_CODERANGE(str) != ENC_CODERANGE_7BIT) {
ENC_CODERANGE_CLEAR(str);
}
return str;
}
return Qnil;
}
Like String#chop, except that:
-
Removes trailing characters from
self(not from a copy ofself). -
Returns
selfif any characters are removed,nilotherwise.
Related: see Modifying.