Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Ruby | Array delete_at() operation

  • Last Updated : 31 Jul, 2021

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

Ruby




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

Output : 

delete : 6

delete : 1

delete : 

Code #2 : Example for delete_at() method 



Ruby




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

Output : 

delete : nil

delete : 

delete : cat

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :