Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Removes leading and trailing whitespace from the receiver. Returns the altered receiver, or nil if there was no change.
Refer to String#strip for the definition of whitespace.
"  hello  ".strip!  #=> "hello"
"hello".strip!      #=> nil
               static VALUE
rb_str_strip_bang(VALUE str)
    char *start;
    long olen, loffset, roffset;
    rb_encoding *enc;

    enc = STR_ENC_GET(str);
    RSTRING_GETMEM(str, start, olen);
    loffset = lstrip_offset(str, start, start+olen, enc);
    roffset = rstrip_offset(str, start+loffset, start+olen, enc);

    if (loffset > 0 || roffset > 0) {
        long 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;

Was this page useful?