4 ms·
That's not really true. First, it's entirely a convention and isn't always followed. Second, there are many many methods which mutate objects and don't have a
by michaelmior 8y ago
That's not really true. First, it's entirely a convention and isn't always followed. Second, there are many many methods which mutate objects and don't have a bang. For example, most of the methods which mutate arrays.
- baby 8y agoYeah, the second part of my comment was wishful thinking. It would be cool if it worked similarly to how Golang capitalizes function names to make them public.
- trampi 8y agoAs someone who has worked not very long with Ruby I have also stumbled upon the "!"-methods. It is a great convention - but as already noted, not consequently used. Have a look at the insert method on array: insert(index, obj...) -> ary From the signature and the convention one could think, that it returns a new array with the object(s) added at the specified index. However, insert mutates the array and returns a reference to the just mutated array, probably for chaining. However, a convention that is not completely followed in the standard library like this does more harm than good as it can confuse newcomers. And you can not change it due to backwards compatibility.
- bglusman 8y agoDidn't know whether to respond here or above to parent, but ! In stdlib of Ruby does not indicate mutation but "dangerous version"... Often the same thing but I believe all methods in std lib with a bang also have a noo-bang version, which usually does not mutate receiver, but could also be, for example, File.read vs File.read!... I forget if Ruby has those but Elixir does and they indicate there that one returns an error or ok prefixed tuple but the dangerous version just raises an error if the file isn't present for example.
- michaelmior 8y agoTrue. Unfortunately I think that means ! just becomes very ambiguous since it's not precisely defined. ("Dangerous" means different things to different people.)
- TomK32 8y agoyou better follow the convention then. just like with the ? methods.