element = [["BLUE", "CAT", "BEAR"], ["BALL", "CHAIR", "BOW"], ["CLOWN", "COLA", "PARROT", "LOVE"]]
阵列中显然有十个元素。如何在不展平阵列的情况下找到计数?
答案 0 :(得分:7)
使用Ruby 2.4,
element.sum(&:size) #=> 10
答案 1 :(得分:2)
ary = [["BLUE", "CAT", "BEAR"], ["BALL", "CHAIR", "BOW"], ["CLOWN", "COLA", "PARROT", "LOVE"]]
sum = ary.inject(0) { |tot, e| tot + e.size }
=> 10
答案 2 :(得分:2)
ary.map(&:count).inject(:+)
#⇒ 10