Anyone ever confused by Array#slice! method?

I just realized a minute ago why every time I
use the slice! method, one or two of my neurons
misfire.

All “bang” methods modify the receiver (though
the converse is not true – let’s not start
that one up again).

And most of them return that modified receiver…
but not the slice! method.

I see why this is, and I’m not complaining.

And of course, now that I know why it confuses
me, it never will again. Nice feeling! :slight_smile:

Just for those who are keeping score, here’s
some Ruby-core trivia:

  1. The String#slice! method works the same way
    as Array#slice!, thank goodness (and Matz).

  2. Offhand I don’t see any other bang methods
    in the core that behave that way.

  3. Some bang methods return nil if the object is
    not actually changed: gsub!, delete!, others.

  4. Now here’s a trivia question. What bang method
    appears in two different core modules and (it
    could be argued) does not really change its
    receiver? (OK, in a way it does. Don’t quibble with
    me.)

Just some midnight ramblings. Ignore at your leisure.

Cheers,
Hal

Hi,

  1. Some bang methods return nil if the object is
    not actually changed: gsub!, delete!, others.

All but reverse!, sort!, and exit!

  1. Now here’s a trivia question. What bang method
    appears in two different core modules and (it
    could be argued) does not really change its
    receiver? (OK, in a way it does. Don’t quibble with
    me.)

exit! does not modify anything. Its ! means “more dangerous” than non
bang version.

						matz.
···

In message “Anyone ever confused by Array#slice! method?” on 03/03/27, “Hal E. Fulton” hal9000@hypermetrics.com writes:

But of course you know all this, Matz. :slight_smile:

I didn’t mean to induce you to reply to
this. But thanks.

Hal

···

----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, March 27, 2003 1:44 AM
Subject: Re: Anyone ever confused by Array#slice! method?

  1. Some bang methods return nil if the object is
    not actually changed: gsub!, delete!, others.

All but reverse!, sort!, and exit!

  1. Now here’s a trivia question. What bang method
    appears in two different core modules and (it
    could be argued) does not really change its
    receiver? (OK, in a way it does. Don’t quibble with
    me.)

exit! does not modify anything. Its ! means “more dangerous” than non
bang version.