I was always under impression that bang version of Array
methods are only different in the way that they modify object in place.
Turns out, some of them like compact!
, reject!
, flatten!
, uniq!
also return nil
if no changes were made bang bang bang
Check out this:
ruby-1.8.7-p302 > [1,[2]].flatten!
=> [1, 2]
ruby-1.8.7-p302 > [1,2].flatten!
=> nil
ruby-1.8.7-p302 > [1,[2]].flatten
=> [1, 2]
ruby-1.8.7-p302 > [1,2].flatten
=> [1, 2]
Or this:
ruby-1.8.7-p302 > [1,2,nil].compact!
=> [1, 2]
ruby-1.8.7-p302 > [1,2].compact!
=> nil
ruby-1.8.7-p302 > [1,2,nil].compact
=> [1, 2]
ruby-1.8.7-p302 > [1,2].compact
=> [1, 2]
This costed me about an hour of madness debugging and I must say, so far, makes no sense at all.
On the other hand, if they did it this way, there has got to be a reason. This assumption could be very very far from truth (I know, I am a programmer), but since this behaviour is kept in the latest ruby version, I'd assume there is one after all.
Any ideas what it might be?