Depending on the arguments passed to the Ruby Array#count method, it can be used in the following cases:
Please note that Array#count is different from Array#size and Array#length methods, which are specifically used for getting the length of an array.
#Counting Total Items
When used without arguments, it counts the total number of items in an array:
puts [3, 3, 4, 3, 3, 5, 5].count #=> 7
puts [].count #=> 0
#Counting Specific Values
When used with a numeric argument, it counts the number of times the specific value occurs in an array:
puts [3, 3, 4, 3, 3, 5, 5].count(3) #=> 4
puts [].count(3) #=> 0
#Conditional Counting
When used with a block, it counts the total number of items in an array that satisfy a condition based on the block:
puts [3, 3, 4, 3, 3, 5, 5].count { | item | item < 4 } #=> 4
puts [].count { | item | item < 4 } #=> 0