Related Articles

Related Articles

Ruby | Array delete_at() operation
  • Last Updated : 08 Jan, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
# prinitng if no such element is found
puts "delete : #{b.delete_at(0)}\n\n"
  
# delete
puts "delete : #{c.delete_at(20)}\n\n"

chevron_right


Output :

delete : 6

delete : 1

delete : 

Code #2 : Example for delete_at() method

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

delete : nil

delete : 

delete : cat



My Personal Notes arrow_drop_up
Recommended Articles
Page :