Re:Rails is not written in Ruby
by int_19h on Friday February 04, 2022 @06:38PM
(#30215110)
sorted() will return a new collection. You still need to do foo.sort() in Python if you want to sort in-place.
[ Parent
| on HN ]
Don't mind me, just adding the equivalent ruby code for readers to compare:
array.sort # returns a new collection
array.sort! # sorts in place
(Ruby convention is to add a ! when methods directly modify the object they're called on.)
Re:Rails is not written in Ruby
by dragonwriter on Friday February 04, 2022 @10:01PM
(#30216677)
> Ruby convention is to add a ! when methods directly modify the object they're called on
No, Ruby convention is to add a “!” when there is a “less safe” version of an operation, for which a “more safe” version exists without a “!”.
Mutating versions of methods where a nonmutating version also exists are one common example, but not the only one, and mutating methods without a nonmutating counterpart don't get “!”.