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 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :



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

delete : [4, 88, 9] 

Code #2 : Example for delete_if() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

delete : ["dog"]

delete : ["cow", "dog"]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.