Ruby | Array compact!() operation Last Updated : 08 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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: removes all the nil values from the array. nil - if there is no nil value in the array Code #1 : Example for compact!() method Ruby # Ruby code for compact!() method # showing how to remove nil values # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [5, 4, 1, 88, 9] # declaring array c = [18, 22, nil, 40, 50, 6] # removing nil value from array puts "removing nil value : #{a.compact!}\n\n" # removing nil value from array puts "removing nil value : #{b.compact!}\n\n" # removing nil value from array puts "removing nil value : #{c.compact!}\n\n" Output : removing nil value : [18, 22, 33, 5, 6] removing nil value : removing nil value : [18, 22, 40, 50, 6] Code #2 : Example for compact!() method Ruby # Ruby code for compact!() method # showing how to remove nil values # declaring array a = ["abc", "nil", "dog"] # declaring array b = ["cow", nil, "dog"] # declaring array c = ["cat", nil, nil] # removing nil value from array puts "removing nil value : #{a.compact!}\n\n" # removing nil value from array puts "removing nil value : #{b.compact!}\n\n" # removing nil value from array puts "removing nil value : #{c.compact!}\n\n" Output : removing nil value : removing nil value : ["cow", "dog"] removing nil value : ["cat"] Comment More infoAdvertise with us Next Article Ruby | Array compact!() operation M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Array-class Similar Reads 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 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 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, 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 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 Like