Remove "empty" elements from a multidimensional array

I have a multidimensional array similar to this

[ [[]], [[1], [2]], [[1, 2]] ] 

What is the best way to remove an empty array?

Right now I'm just doing array[1..-1] to remove the first element, but I would like to get a more reliable way to do this.

+4
source share
1 answer

Flatten each array, and if there are no elements in it, delete it.

 arr = [ [[]], [[1], [2]], [[1, 2]] ] arr = arr.delete_if { |elem| elem.flatten.empty? } # => [[[1], [2]], [[1, 2]]] 
+8
source

All Articles