Ruby | Array collect!() operation Last Updated : 08 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 elements Code #1 : Example for collect!() method Ruby # Ruby code for collect!() method # declaring array a = ["cat", "rat", "geeks"] # invoking block for each element # returning elements that don't follow puts "collect a : #{a.collect! {|x| x + "!" }}\n\n" puts "collect a : #{a.collect! {|x| x + "_at" }}\n\n" Output : collect a : ["cat!", "rat!", "geeks!"] collect a : ["cat!_at", "rat!_at", "geeks!_at"] Code #2 : Example for collect!() method Ruby # Ruby code for collect!() method # declaring array a = [1, 2, 3, 4] # invoking block for each element # returning elements that don't follow puts "collect a : #{a.collect! {|x| x + 1 }}\n\n" puts "collect a : #{a.collect! {|x| x - 5*7 }}\n\n" Output : collect! a : [2, 3, 4, 5] collect! a : [-33, -32, -31, -30] Comment More infoAdvertise with us Next Article Ruby | Array collect!() operation M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Array-class Similar Reads 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 concat() operation Array#concat() : concat() is a Array class method which returns the array after appending the two arrays together. Syntax: Array.concat() Parameter: Arrays to be combined Return: Append the two arrays Code #1 : Example for concat() method Ruby # Ruby code for concat() method # adding elements at the 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. 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 count() operation 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 : Exa 2 min read Like