Array#count() : count() is a Array class method which returns the number of elements in the array. It can also find the total number of a particular element in the array.
Syntax: Array.count() Parameter: obj - specific element to found Return: removes all the nil values from the array.
Code #1 : Example for count() method
# Ruby code for count() method # to count elements # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, nil, nil, 50, 6] # counting total elements puts "counting : #{a.count}\n\n" # counting 1 puts "counting : #{b.count(1)}\n\n" # counting 'nil' puts "counting : #{c.count(nil)}\n\n" |
chevron_right
filter_none
Output :
counting : 6 counting : 3 counting : 2
Code #2 : Example for count() method
# Ruby code for count() method # to count elements # declaring array a = ["abc", "nil", "dog"] # declaring array b = ["cow", nil, "dog"] # declaring array c = ["cat", nil, nil] # counting total elements puts "counting : #{a.count}\n\n" # counting 1 puts "counting : #{b.count(1)}\n\n" # counting 'nil' puts "counting : #{c.count(nil)}\n\n" |
chevron_right
filter_none
Output :
counting: 3 counting : 0 counting : 2
Recommended Posts:
- Ruby | Array cycle() operation
- Ruby | Array delete() operation
- Ruby | Array delete_at() operation
- Ruby | Array delete_if() operation
- Ruby | Array compact() operation
- Ruby | Array compact!() operation
- Ruby | Array concat() operation
- Ruby | Array dig() operation
- Ruby | Array drop() operation
- Ruby | Array combination() operation
- Ruby | Array collect() operation
- Ruby | Array collect!() operation
- Ruby | Array clear() operation
- Ruby | Array bsearch_index() operation
- Ruby | Array at() operation
- Ruby | Array bsearch() operation
- Ruby | Array rassoc() operation
- Ruby | Array class drop_while() operation
- Ruby | Array class each() operation
- Ruby | Array class each_index() operation
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.

