Open In App

Ruby | Array delete() operation

Last Updated : 21 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Array#delete() : delete() is a Array class method which returns the array after deleting the mentioned elements. It can also delete a particular element in the array. 

Syntax:  Array.delete()

Parameter:  obj - specific element to delete

Return:  last deleted values from the array. 

Code #1 : Example for delete() method

Ruby




# Ruby code for delete() method
# to delete 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]
 
# delete 5
puts "delete : #{a.delete(5)}\n\n"
 
# delete 10
# printing if no such element is found
puts "delete : #{b.delete(10){"no such element"}}\n\n"
 
# delete 20
puts "delete : #{c.delete(20)}\n\n"


Output : 

delete : 5

delete : no such element

delete : 

Code #2 : Example for delete() method 

Ruby




# Ruby code for delete() method
# to delete elements
 
# declaring array
a = ["abc", "nil", "dog"]
 
# declaring array
b = ["cow", nil, "dog"]
 
# declaring array
c = ["cat", nil, nil]
 
# delete
puts "delete : #{a.delete("abc")}\n\n"
 
# delete
# printing if no such element is found
puts "delete : #{b.delete("geek"){"no such element"}}\n\n"
 
# delete
puts "delete : #{c.delete("cat")}\n\n"


Output : 

delete : abc

delete : no such element

delete : cat

 



Similar Reads

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 block Return: array by calling the given block each t
2 min read
Ruby | Array delete_at() operation
Array#delete_at() : delete_at() is a Array class method which deletes the arrays elements at the mentioned index. Syntax: Array.delete_at() Parameter: index - value at specific index value to delete Return: deleted element, nil - if the index value is out of range Code #1 : Example for delete_at() method C/C++ Code # Ruby code for delete_at() metho
2 min read
Ruby | Array delete_if() operation
Array#delete_if() : delete_if() is a Array class method which deletes the arrays elements for which the block condition satisfies. Syntax: Array.delete_if() Parameter: block - condition for deleting the elements. Return: array after deleting the elements Code #1 : Example for delete_if() method C/C++ Code # Ruby code for delete_if() method # declar
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. 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() method # Ruby code for compact() method # showi
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: removes all the nil values from the array. nil -
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 code for concat() method # adding elements at the end # declaring array a = [18, 22, 33, nil, 5, 6] # de
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 : Example for count() method # Ruby code for count() me
2 min read
Ruby | Array dig() operation
Array#dig() : dig() is a Array class method which extracts the specific element out of the high dimension sequences. Syntax: Array.dig() Parameter: element position. Return: element from a specific location in sequence, returning nil if any intermediate step is nil. Code #1 : Example for dig() method # Ruby code for dig() method # declaring array a
1 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 code for drop() method # declaring array a = [18, 22, 33, nil, 5, 6] #
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 array. Code #1 : Example for combination() method #
1 min read