Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Ruby convention is not followed for Array#keep_if?

Tags:

ruby

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?

like image 958
Sharvy Ahmed Avatar asked Oct 31 '15 08:10

Sharvy Ahmed


2 Answers

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

like image 94
spickermann Avatar answered Oct 10 '22 02:10

spickermann


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.

like image 37
rohit89 Avatar answered Oct 10 '22 02:10

rohit89