element = [["BLUE", "CAT", "BEAR"], ["BALL", "CHAIR", "BOW"], ["CLOWN", "COLA", "PARROT", "LOVE"]]
There are obviously ten elements across the arrays. How do I find the count without flattening the array?
With Ruby 2.4,
element.sum(&:size) #=> 10
ary = [["BLUE", "CAT", "BEAR"], ["BALL", "CHAIR", "BOW"], ["CLOWN", "COLA", "PARROT", "LOVE"]]
sum = ary.inject(0) { |tot, e| tot + e.size }
=> 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With