Count with block : count « Array « Ruby






Count with block


a = [1,1,2,3,5,8]

a.count(1)                # => 2: two elements equal 1
a.count {|x| x % 2 == 1}  # => 4: four elements are odd

 

Related examples in the same category