Just as a heads-up, since I experienced this myself.
In Ruby, !
mutates the object and returns it. Otherwise it will return nil
.
So, if you are doing some kind of operations on an array for example, and call the method .compact!
and there is nothig to compact, it will return nil
.
Example:
arr = [1, 2, 3, nil]
arr.compact!
=> [1, 2, 3]
Run again arr.compact!
=> nil
It is better to explicitly return again the array arr
if you need to use it down the line, otherwise you will get the nil
value.
Example:
arr = [1, 2, 3]
arr.compact! => nil
arr # to get the value of the array