For the first part, all you need is Array#transpose
.
array.transpose
#=> [[1,1,0],[1,1,0],[1,1,0],[0,0,1],[0,0,1]]
then you can do the following
.map {|arr| arr.join.scan(/11+/)}
to count the consecutive ones. The join
converts each subarray to a string, then scan
checks for two or more consecutive 1
s.
Altogether:
array.transpose.map {|arr| arr.join.scan(/11+/)}
#=> [["11"], ["11"], ["11"], [], []]
If you want to remove the empty arrays, @Doorknob notes that you can append a reject
:
array.transpose.map {|arr| arr.join.scan(/11+/)}.reject(&:empty?)
#=> [["11"], ["11"], ["11"]]