3 Answers
3

since nil coerces to float as 0. This doesn't handle negative values, but since nil is only a single instance of a class called NilClass, now as with all classes in Ruby we can open it an let it learn a littly maths:

For your example this is obviously more complicated than compact, but if you want to sort the array and keep the nil values it's a handy trick. Or if you want to sort in a strange way, like zeroes to the end: