📜  红宝石 |数组 delete_at() 操作

📅  最后修改于: 2022-05-13 01:55:33.220000             🧑  作者: Mango

红宝石 |数组 delete_at() 操作

Array#delete_at() :delete_at()是一个 Array 类方法,用于删除上述索引处的数组元素。

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

代码 #1:delete_at() 方法的示例

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"


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"


输出 :

delete : 6

delete : 1

delete : 

代码 #2:delete_at() 方法的示例

红宝石

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

输出 :

delete : nil

delete : 

delete : cat