Solution to the overlapping intervals problem in ruby