Behaviour of Array bang methods

Viewed 2792

Some bang version of Array methods are like compact!, reject!, flatten!, uniq! return nil if no changes were made:

[1,[2]].flatten!
# => [1, 2]
[1,2].flatten!
# => nil
[1,[2]].flatten
# => [1, 2]
[1,2].flatten
# => [1, 2]

[1,2,nil].compact!
# => [1, 2]
[1,2].compact!
# => nil
[1,2,nil].compact
# => [1, 2]
[1,2].compact
# => [1, 2]

If they did it this way, there has to be a reason. Any ideas what it might be?

2 Answers
Related