We can use Enumerable#chunk for this:
arr = [nil, nil, nil, nil, 5, 6, 7, 8, 9, 10, 11, nil, nil, nil, nil, nil, 17,
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 50, 51]
arr.chunk(&:nil?).reject(&:first).map(&:last)
#=> [[5, 6, 7, 8, 9, 10, 11], [17], [50, 51]]
The steps:
enum = arr.chunk(&:nil?)
#=> #<Enumerator: #<Enumerator::Generator:0x007fd303042440>:each>
We can examine the elements of the enumerator enum
by converting it to an array:
enum.to_a
#=> [[true, [nil, nil, nil, nil]],
# [false, [5, 6, 7, 8, 9, 10, 11]],
# [true, [nil, nil, nil, nil, nil]],
# [false, [17]],
# [true, [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]],
# [false, [50, 51]]]
Two more steps:
a = enum.reject(&:first)
#=> [[false, [5, 6, 7, 8, 9, 10, 11]],
# [false, [17]],
# [false, [50, 51]]]
a.map(&:last)
#=> [[5, 6, 7, 8, 9, 10, 11], [17], [50, 51]]
Edit: Thanks to @Stefan, today I learned something new about the way chunk
handles nil
s. As he suggests, we can simplify this to:
arr.chunk { |e| e && false }.map(&:last)
#=> [[5, 6, 7, 8, 9, 10, 11], [17], [50, 51]]
Well, maybe that's not exactly what he said, but it works just as well. (My variant is more of a head-scratcher.)