# ! semantics (was: Re: Does an Array#apply make any sense at

**URL:** <https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045>\
**Category:** Ruby\
**Created:** [August 14, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045 "2007-08-14T16:49:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 14, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/1 "2007-08-14T16:49:25Z")

</div>

Hi –

On Tue, 14 Aug 2007, Simon K. wrote:

> That is true for Array#delete. delete is always destructive, there is no  
> need to flag that.

And push, pop, \<\<, concat, replace, clear… There’s definitely no  
general guideline that destructive methods end in !.

(Of course String has both delete and delete! 🙂

> But there is no need for your method to be destructive, so you need to  
> tell the world about it.
> 
> > I think this makes sense. If unpaired dangerous methods have !, it  
> > sort of suggests that any time there isn’t a !, the method is  
> > non-dangerous, which in turn suggests non-destructive… and that  
> > isn’t true.
> 
> It should simply be clear from the name[0] if a method is destructive.  
> Often you need the ! for that, in some cases it’s obvious without the !.

I guess I take my cue from the Ruby core/standard language, where  
there’s no use of !, as far as I know, except to distinguish a  
“dangerous” method from its non-dangerous partner. I don’t know of any  
case where a method is just considered “dangerous” in the abstract,  
without comparison to another method.

Basically, I can’t find any object for which this:

obj.methods.grep(/!/).detect {|m| not obj.respond\_to?(m.delete(’!’)) }

or similar tests with private methods, etc., returns a value.

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/2 "2007-08-15T13:06:14Z")

</div>

- [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (16:48) schrieb:

> > That is true for Array#delete. delete is always destructive, there is no  
> > need to flag that.
> 
> And push, pop, \<\<, concat, replace, clear…

Which all have to be modifying. With the exception of concat. (The Unix  
cat command isn’t modifying.)

> > It should simply be clear from the name[0] if a method is destructive.  
> > Often you need the ! for that, in some cases it’s obvious without the !.
> 
> I guess I take my cue from the Ruby core/standard language, where  
> there’s no use of !, as far as I know, except to distinguish a  
> “dangerous” method from its non-dangerous partner.

That’s because of the completeness of the library. Whenever there can be  
a non-modifying partner, there is one.

When you just define a modifying method, is not right to name it without  
a ! just because you choose not to define a non-modifying version.

What if some other developer wants to add the non-modifying version?  
He would have to rename your version.

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 15, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/3 "2007-08-15T13:50:21Z")

</div>

Hi –

On Wed, 15 Aug 2007, Simon K. wrote:

> > > It should simply be clear from the name[0] if a method is destructive.  
> > > Often you need the ! for that, in some cases it’s obvious without the !.
> > 
> > I guess I take my cue from the Ruby core/standard language, where  
> > there’s no use of !, as far as I know, except to distinguish a  
> > “dangerous” method from its non-dangerous partner.
> 
> That’s because of the completeness of the library. Whenever there can be  
> a non-modifying partner, there is one.

! does not mean modifying; it means “dangerous”.

> When you just define a modifying method, is not right to name it without  
> a ! just because you choose not to define a non-modifying version.

I would never add a ! to a method name solely because it changes the  
receiver. That’s a re-invention of what ! is for.

> What if some other developer wants to add the non-modifying version?  
> He would have to rename your version.

I think that’s a solution in search of a problem. Just name your  
methods clearly, without !. Then, if you (or someone else) want to add  
a “dangerous” version, you can write the ! method.

Also – once again – ! does not mean that the receiver is changed. It  
means “dangerous”; it’s a warning to the programmer that the method is  
going to do something that the non-! version doesn’t do, and that  
might involve the unexpected.

! is not a general-purpose receiver-modifying flag, and I would very  
strongly discourage its adaptation to that use. It’s not necessary,  
and it dilutes the original purpose of !.

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 1:05am UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/4 "2007-08-16T01:05:04Z")

</div>

On 8/15/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > there’s no use of !, as far as I know, except to distinguish a  
> > > “dangerous” method from its non-dangerous partner.
> > 
> > That’s because of the completeness of the library. Whenever there can be  
> > a non-modifying partner, there is one.
> 
> ! does not mean modifying; it means “dangerous”.

Just thought that I’d mention the usual example of the difference is  
exit  
vs. exit! Neither is a “modifying” version of each other.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 4:05am UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/5 "2007-08-16T04:05:47Z")

</div>

- [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (13:48) schrieb:

> > That’s because of the completeness of the library. Whenever there can be  
> > a non-modifying partner, there is one.
> 
> ! does not mean modifying; it means “dangerous”.

Most methods flagged with a ! are dangerous because they modify their  
receiver.

> > When you just define a modifying method, is not right to name it without  
> > a ! just because you choose not to define a non-modifying version.
> 
> I would never add a ! to a method name solely because it changes the  
> receiver. That’s a re-invention of what ! is for.

No, you add it because someone might else assume it is a non-modifying  
method, that is what makes it dangerous.

Array#pop is modifying but not dangerous, because everyone expects it to  
be modifying.

> > What if some other developer wants to add the non-modifying version?  
> > He would have to rename your version.
> 
> I think that’s a solution in search of a problem.

No, that’s a problem.

> Just name your methods clearly, without !. Then, if you (or someone  
> else) want to add a “dangerous” version, you can write the ! method.

You are the one who’s proposing that something isn’t dangerous because  
there currently isn’t a less dangerous version.

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 4:20am UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/6 "2007-08-16T04:20:48Z")

</div>

Hi –

On Thu, 16 Aug 2007, Simon K. wrote:

> - [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (13:48) schrieb:
> 
> > > That’s because of the completeness of the library. Whenever there can be  
> > > a non-modifying partner, there is one.
> > 
> > ! does not mean modifying; it means “dangerous”.
> 
> Most methods flagged with a ! are dangerous because they modify their  
> receiver.

Many of them are, but there’s no cause-and-effect between modifying  
and !. If there were, there would be !'s on all modifying methods. I  
think we should really put that to rest; the convention just doesn’t  
work that way, and never has.

> be modifying.  
> If the name doesn’t connote destructiveness, then you should change  
> the name. For example, I would not use the name “last” instead of  
> “pop”, because “last” does not connote destructiveness.

But I also wouldn’t use “last!”. Just adding a ! does not change a bad  
name for a destructive method into a good name. It just adds a !.

> > > What if some other developer wants to add the non-modifying version?  
> > > He would have to rename your version.
> > 
> > I think that’s a solution in search of a problem.
> 
> No, that’s a problem.

Not really. If I have a “pop” method, and someone wants to write  
“last” (i.e., return the last element but not destructively), they  
don’t have to write “pop!-minus-!”. They should choose the best name,  
which may have nothing to do with the name I gave the destructive  
method.

In other words, m/m! is not the only possible ratio between methods.  
There’s also m/n, which can be just as close as m/m!. pop/last is an  
example; so is concat/+ (also for arrays).

> > Just name your methods clearly, without !. Then, if you (or someone  
> > else) want to add a “dangerous” version, you can write the ! method.
> 
> You are the one who’s proposing that something isn’t dangerous because  
> there currently isn’t a less dangerous version.

I don’t think I can quite follow all the convolutions we’re getting  
into 🙂 I’ve posted something about the ! on my blog, if anyone’s  
interested:

[http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist](http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist)

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2007, 4:45am UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/7 "2007-08-16T04:45:50Z")

</div>

On Aug 15, 2007, at 16:03, Logan C. wrote:

> > ! does not mean modifying; it means “dangerous”.
> 
> Just thought that I’d mention the usual example of the difference  
> is exit  
> vs. exit! Neither is a “modifying” version of each other.

Instead of modifying anything, #exit! doesn’t clean up after itself  
(run at\_exit handlers, attempt to run finalizers, raise SystemExit,  
close file descriptors). All of these things may be dangerous.

It is #exit that does the modifying of things.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 17, 2007, 2:50am UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/8 "2007-08-17T02:50:40Z")

</div>

Hi –

On Fri, 17 Aug 2007, Simon K. wrote:

> BTW: Is there any reason you don’t mention your whole name in your posts?

It’s all there, just spread out between the email address and the  
sign-off 🙂 It’s possible that I used to have it in my .sig; I can’t  
remember.

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 17, 2007, 2:35am UTC](https://www.ruby-forum.com/t/semantics-was-re-does-an-array-apply-make-any-sense-at/112045/9 "2007-08-17T02:35:39Z")

</div>

- [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (04:19) schrieb:

> > Most methods flagged with a ! are dangerous because they modify their  
> > receiver.
> 
> Many of them are, but there’s no cause-and-effect between modifying  
> and !.

You are hitting a strawman.

> > Array#pop is modifying but not dangerous, because everyone expects it to  
> > be modifying.
> 
> If the name doesn’t connote destructiveness, then you should change  
> the name.

Obviously the many examples of pairs with and without ! don’t follow  
that guideline.

> > > > What if some other developer wants to add the non-modifying version?  
> > > > He would have to rename your version.
> > > 
> > > I think that’s a solution in search of a problem.
> > 
> > No, that’s a problem.
> 
> Not really. If I have a “pop” method,

We have an “apply” method.

> In other words, m/m! is not the only possible ratio between methods.

Who said it was?

> There’s also m/n, which can be just as close as m/m!. pop/last is an  
> example; so is concat/+ (also for arrays).

And there is m/m!. If you write m!, name it m!, not m.

> I don’t think I can quite follow all the convolutions we’re getting  
> into 🙂 I’ve posted something about the ! on my blog, if anyone’s  
> interested:
> 
> [http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist](http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist)

You write “create the traditional pair of methods”, that’s right. But  
now  
I’m too lazy to write the simple non-modifying wrapper. That’s not a  
good  
reason to omit the !.

BTW: Is there any reason you don’t mention your whole name in your  
posts?

mfg, simon … someone wrote a blog because of my posts
