Ruby | Array count() operation Last Updated : 08 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 # 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" Output : counting : 6 counting : 3 counting : 2 Code #2 : Example for count() method Ruby # 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" Output : counting: 3 counting : 0 counting : 2 Comment More infoAdvertise with us Next Article Ruby | Array count() operation M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Array-class Similar Reads Ruby | Array at() operation Array#at() : at() is an Array class method which returns the element at the specific argumented index value. Syntax: Array.at() Parameter: - Arrays to search elements. - index to search Return: Array element at a specific index value Code #1 : Example for at() method Ruby # Ruby code for at() method 1 min read Ruby | Array collect() operation Array#collect() : collect() is an Array class method which invokes the argument block once for each element of the array. A new array is returned which has the value returned by the block. Syntax: Array.collect() Parameter: Arrays in which we want elements to be invoked Return: array with all the en 1 min read Ruby | Array collect!() operation Array#collect!() : collect!() is an Array class method which invokes the argument block once for each element of the array, replacing the element with the value returned by the block Syntax: Array.collect!() Parameter: Arrays in which we want elements to be invoked Return: array with all the envoked 1 min read Ruby | Array compact!() operation Array#compact! () : compact! () is a Array class method which returns the array after removing all the 'nil' value elements (if any) from the array. If there are no nil values in the array it returns back the nil value. Syntax: Array.compact!() Parameter: Array to remove the 'nil' value from. Return 2 min read Ruby | Array compact() operation Array#compact () : compact () is a Array class method which returns the array after removing all the 'nil' value elements (if any) from the array. Syntax: Array.compact() Parameter: Array to remove the 'nil' value from. Return: removes all the nil values from the array. Code #1 : Example for compact 2 min read Ruby | Array drop() operation Array#drop() : drop() is a Array class method which drops first 'n' elements from the array and returns the remaining elements. Syntax: Array.drop() Parameter: 'n' - no. of elements to drop. Return: array after removing first n elements Code #1 : Example for drop() method Ruby # Ruby code for drop() 1 min read Ruby | Array combination() operation Array#combination() : combination() is an Array class method which invokes with a block yielding all combinations of length 'n' of elements of the array. Syntax: Array.combination() Parameter: Arrays in which we want elements to be invoked Return: all combinations of length 'n' of elements of the ar 1 min read Ruby | Array cycle() operation Array#cycle() : cycle() is a Array class method which returns the array by calling the given block each time for every element in the array 'n' no. of times and if 'nil' is given, then it will call it for forever. Syntax: Array.cycle() Parameter: block - condition n - number of times to call the blo 2 min read Ruby | Array map!() operation Array#map!() : map!() is a Array class method which returns a new array containing the values returned by the block. Syntax: Array.map!() Parameter: Array Return: a new array containing the values returned by the block. Example #1 : Ruby # Ruby code for map!() method # declaring array a = [18, 22, 3 2 min read Like