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