Open In App
Related Articles

Ruby | Enumerable count() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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:
# Ruby program for count method in Enumerable
  
# Initialize
enu = [12, 18]
  
# returns enumerator
res = enu.count

                    
Output:
2
Example 2:
# Ruby program for count method in Enumerable
  
# Initialize
enu = [12, 18, 12]
  
# returns enumerator
res = enu.count(12)

                    
Output:
2
Example 3:
# Ruby program for count method in Enumerable
  
# Initialize
enu = [12, 18, 16, 18]
  
# returns enumerator
res = enu.count { |el| el > 13}

                    
Output:
3


Last Updated : 18 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads