4 ms·
It's neither. It's reject! because reject already exists. Ruby isn't Scheme: a ! suffix doesn't have a simple conventional meaning. (Unlike a ? suffix.) The st
by parenthephobia 10y ago
It's neither. It's reject! because reject already exists. Ruby isn't Scheme: a ! suffix doesn't have a simple conventional meaning. (Unlike a ? suffix.)
The standard library has lots of side-effecting methods which don't come with ! suffix. On the same Array type that has reject!, there's delete, pop, push, among others. Only some of them have pure equivalents - e.g. last for pop.
- 8fGTBjZxBcHq 10y agoMethods in ruby only have the bang if there's another version of the method and this one might surprise you. Push and pop don't have it because they mutate the array like they do in almost every non-hardcore-functional language, so that's the expected behavior. Things like map! and reject! have them because there _is_ another version, and _this_ one might bite you. It has nothing to do with side-effects or immutability per se, that's just the most common case.
- smitherfield 10y agoMy impression is that the convention is a ! suffix means both that 1) There's another, similar, method without the suffix. 2) The ! version has some side effect (usually mutating something or throwing an exception) that the non-! doesn't, which doesn't mean that the non-! has no side effects (`ApplicationRecord#save` and `#save!` both have side effects, but only `#save!` throws on failure). — Is there any idiomatic Ruby library where ! methods don't follow this convention?