Methods with and without!

Most but not all bang methods modify the receiver in-place. But that

is

not the "meaning" of the bang.

The implication of the bang is that the method is more dangerous or

more of

a special case. The exit! method, for example, exits without running

the

exit handlers.

Another, hard learned, lesson is that bang methods may also return
something else than the reciever (e.g. nil to signify no change). This
is needed since you no longer have the original object to compare to.
The non-bang variants return something more appropriate (like a copy of
the reciever).

This effectively means you must never chain method calls past bang
methods.

array.select{|x| x>2 }.sort!.reverse! # very bad and may raise a
                                             # "nil does not respond to
..."
                 # exception
array = array.select{|x| x>2 }.sort.reverse # the safer way to do this

It would seem to me that this *may* ought to be *must*, albeit it is not
enforced by the language, but I wonder if it could be?

···

On Tuesday 21 September 2004 01:48 am, Mehr, Assaph (Assaph) wrote:

Another, hard learned, lesson is that bang methods may also return
something else than the reciever

--
( o _ カラチ
// trans.
/ \ transami@runbox.com

I don't give a damn for a man that can only spell a word one way.
-Mark Twain