I have this intermittent array:
a = [1, 2, 3, 7, 8, 10, 11, 12]
I need it to be an array of contiguous arrays:
[[1, 2, 3], [7, 8], [10, 11, 12]]
I look at the original, comparing each value with the last to create new arrays:
parts = []
last = nil
a.each do |n|
parts.push [] if last.nil? || last+1 != n
parts.last.push n
last = n
end
He feels dirty and non-ruby-like. I am interested in finding a clean, elegant solution.
source
share