A destructive method permanently changes the state of its receiver instead of returning a changed copy. Ruby commonly pairs destructive methods with nondestructive alternatives. section 7.3.1
>> str = "Hello"
=> "Hello"
>> str.upcase
=> "HELLO"
>> str
=> "Hello" 1
>> str.upcase!
=> "HELLO"
>> str
=> "HELLO" 2
Compare str.upcase, which returns a new uppercase string while preserving str, with str.upcase!, which changes str in place. section 7.3.1
Recognize destructive behavior through these contrasts:
- Choose the nondestructive form when later code may rely on the original object; choose the destructive form when changing that object is intentional. section 7.3.1
- Expect pairs such as sort/sort!, strip/strip!, and reverse/reverse!; the non-bang method produces a new object, while the bang method operates on the receiver. section 7.3.1
- Do not treat ! as a synonym for destructive: bang methods signal potentially dangerous behavior, and some destructive methods, such as clear and push, have no bang. section 7.3.2
- Balance allocation costs against aliasing risks: new objects can consume resources during large-file processing, while in-place changes can affect other users of the receiver. section 7.3.1
Uppercasing names without changing stored data
For each name in an array, assign name.upcase to capped and use capped for printed output. The original name remains available for later processing; using name.upcase! would alter it in place. section 7.3.1
