7 ms·
That's why I really like the ! In Ruby. If used in a function name it indicates that it will mutate its arguments. It's so straightforward instead of just not k
by baby 8y ago
That's why I really like the ! In Ruby. If used in a function name it indicates that it will mutate its arguments. It's so straightforward instead of just not knowing or having to use references or pointers.
- michaelmior 8y agoThat'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.
- bjoli 8y agoThis is something ruby inherited from scheme. Scheme library devs are however a lot more militant about it than ruby devs, so everything that mutates has a ! after it.
- shakna 8y agoIt's one of the small things about Scheme I love. foo! for mutating. foo? for predicates. foo->bar for type conversations.
- stilley2 8y agoJulia does this too. It's brilliant. Side note: I just tried solving an actual problem in julia for the first time. That language does a lot of stuff right, at least for my definition of right.
- dpwm 8y agoPytorch does something similar, except with an underscore at the end of the name. As the arguments are tensors, it's a really handy convention and the API communicates whether a method or function modifies an argument. I don't believe this was always the case, so it's probably caused a lot of deprecation warnings for some people.
- bglusman 8y agoAlso commented below but the idea here was not actually mutation but safe and dangerous versions of methods... E.g, below insert is mentioned and it has no safer version so it has no ! version... Elixir adopted this convention from Ruby also, but there is never any mutation in Elixir of course as BEAM doesn't allow it.
- phyzome 8y agoThis is why I really like Rust's "mut" keyword that enforces whether a function argument or method target is mutable. :-)