There is a Ruby convention for method naming using bang(!
). The convention is if the method changes self
, we use bang to let know others about the self modifying bahaviour.
For example Array#select
doesn't change self
, but Array#select!
does. But Array#keep_if
does change self. There is nothing called Array#keep_if!
.
What might be the reason for not following the convention?
Matz, the creator of Ruby, wrote:
The bang (!) does not mean "destructive" nor lack of it mean non destructive either. The bang sign means "the bang version is more dangerous than its non bang counterpart; handle with care". Since Ruby has a lot of "destructive" methods, if bang signs follow your opinion, every Ruby program would be full of bangs, thus ugly.
Source: Ruby Forum
This is a common misconception. The bang method is used to distinguish between a dangerous and a safe version of the same method. It does not mean that it will necessarily change its receiver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With