Ruby 数组的delete_at()操作
Array#delete_at() : delete_at() 是一个Array类的方法,它可以将数组中的元素在提到的索引处删除。
语法: Array.delete_at()
参数: index – 要删除的特定索引值的值
返回: 被删除的元素, nil – 如果索引值超出了范围
代码 #1 : 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 : 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