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.
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
array = ["a", "b", "c"]
array.compact!
array.flatten!
array.map! { |x| x.downcase }
array