Ruby simple algorithms and data structure