Find number of arrays in an array which are not empty

I have an array of arrays like this:

[row1, row2, row3]

I need to find how many of those rows are not empty (have some object inside). I know I can do row1.any?, but how would I find how many of those are in the array?

Answers


Use the block form of Array#count:

[row1, row2, row3].count &:any?

What about this?

[row1,row2,row3].select { |arr| arr.any? }.size

You select just the objects (arrays) which have any elements, and then calculate the size of that resulting Array.


And as usual, remember that .any? will not give you a hit if you array is filled with false or nil.

[[false], [nil], [true], []].count &:any? #=> 1 only one non-empty (true)

Perhaps it is better to do:

[[false], [nil], [true], []].reject(&:empty?).count #=> 3 non-empty (true, nil, false)

Need Your Help

dojox.grid.DataGrid dojox.data.HtmlStore

grid dojo

It would be so slick if you could use dojox.grid.DataGrid and dojox.data.HtmlStore together. Maybe there is an easier way to get sortable tables with Dojo, but this is all I can come up with... an...

Accordion adjusting height of the panels in jquery

javascript jquery jquery-ui accordion

I am trying to stop autoresize accordion panel by using this script