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:
# 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
Recommended Posts:
- Ruby | Enumerable any? function
- Ruby | Enumerable first() function
- Ruby | Enumerable map() function
- Ruby | Enumerable all? function
- Ruby | Enumerable none?() function
- Ruby | Enumerable min() function
- Ruby | Enumerable max() function
- Ruby | Enumerable one? function
- Ruby | Enumerable sum() function
- Ruby | Enumerable take() function
- Ruby | Enumerable drop() function
- Ruby | Enumerable sort_by() function
- Ruby | Enumerable take_while() function
- Ruby | Enumerable reverse_each() function
- Ruby | Enumerable cycle() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



