Просто итерируйте по каждой начальной позиции и для каждой начальной позиции по каждой возможной конечной позиции:
arr = ["the", "cat", "sat", "on", "the", "mat"]
(0 ... arr.length).map do |i|
(i ... arr.length).map do |j|
arr[i..j]
end
end.flatten(1)
#=> [["the"], ["the", "cat"], ["the", "cat", "sat"], ["the", "cat", "sat", "on"], ["the", "cat", "sat", "on", "the"], ["the", "cat", "sat", "on", "the", "mat"], ["cat"], ["cat", "sat"], ["cat", "sat", "on"], ["cat", "sat", "on", "the"], ["cat", "sat", "on", "the", "mat"], ["sat"], ["sat", "on"], ["sat", "on", "the"], ["sat", "on", "the", "mat"], ["on"], ["on", "the"], ["on", "the", "mat"], ["the"], ["the", "mat"], ["mat"]]
Требуется ruby 1.8.7+ (или backports) для flatten(1)
.