Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Array delete_if() operation

  • Last Updated : 08 Jan, 2020

Array#delete_if() : delete_if() is a Array class method which deletes the arrays elements for which the block condition satisifies.

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 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 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
Recommended Articles
Page :

Start Your Coding Journey Now!