Why is the Ruby convention not being respected for Array # keep_if?

There is a Ruby convention for naming methods using bang ( ! ). The convention is that if the method modifies self , we use bang to tell others about the self-mutating bahaviour.

For example, Array#select does not change self , but Array#select! does. But Array#keep_if changing itself. Nothing called Array#keep_if! .

What could be the reason for not following the convention?

+5
source share
2 answers

Matz, the creator of Ruby, wrote:

An explosion (!) Does not mean "destructive", or its absence means destructive. The sign of the explosion means that "the version of the explosion is more dangerous than its counterpart to the sky; handle with care." Ruby has many "destructive" methods, if the gang signs follow your opinion, each ruby ​​program would be full of punches, thus ugly.

Source: Ruby Forum

+4
source

This is a common misconception. The bang method is used to distinguish between a dangerous and a safe version of the same method. This does not mean that he will necessarily change the receiver.

0
source

Source: https://habr.com/ru/post/1234909/


All Articles