Ruby

Ruby

Made by DeepSource

Replace methods on array with mutations RB-PR1004

Performance
Major

Methods compact, flatten and map generate a new intermediate array that is discarded. It is faster to mutate the array when we know it's safe.

Bad practice

array = ["a", "b", "c"]
array.compact.flatten.map { |x| x.downcase }

The above example has 3 intermediate arrays generated. Since the array can be mutated safely, it can be written as

Recommended

array = ["a", "b", "c"]
array.compact!
array.flatten!
array.map! { |x| x.downcase }
array