Skip to content
Related Articles

Related Articles

Ruby | Array delete_if() operation

Improve Article
Save Article
  • Last Updated : 13 Jan, 2022
Improve Article
Save Article

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 

Ruby




# Ruby code for delete_if() method
   
# declaring array
a = [18, 22, 33, 23, 5, 6]
   
# declaring array
b = [1, 4, 1, 1, 88, 9]
   
# declaring array
c = [18, 22, 12, 24, 50, 6]
   
# delete
puts "delete : #{a.delete_if{|x| x < 1}}\n\n"
   
# delete
puts "delete : #{b.delete_if{|b| b==1}}\n\n"

Output : 

delete : [18, 22, 33, 23, 5, 6]

delete : [4, 88, 9] 

Code #2 : Example for delete_if() method  

Ruby




# Ruby code for delete_if() method
   
# declaring array
a = ["abc", "geeks", "dog"]
   
# declaring array
b = ["cow", "1", "dog"]
   
   
# delete
puts "delete : #{a.delete_if{|x| x != "dog"}}\n\n"
   
# delete
puts "delete : #{b.delete_if{|b| b=="1"}}\n\n"

Output : 

delete : ["dog"]

delete : ["cow", "dog"]

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!