Ruby | Enumerable count() function
The count() of enumerable is an inbuilt method in Ruby returns the number of elements in the enumerable, or the number of elements that are equal to a given element, or the number of items which satisfies the condition in the given block.
Syntax: block.count { |obj| block } or block.count(element)
Parameters: The function takes a block or an item. If it does not takes any of both, then it returns the number of elements in the enumerable.
Return Value: It returns the count of elements.
Example 1:
enu = [ 12 , 18 ]
res = enu.count
|
Output:
2
Example 2:
enu = [ 12 , 18 , 12 ]
res = enu.count( 12 )
|
Output:
2
Example 3:
enu = [ 12 , 18 , 16 , 18 ]
res = enu.count { |el| el > 13 }
|
Output:
3
Last Updated :
18 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...