我正在尝试删除数组中索引大于某个值的元素。我想要实现类似于以下的操作:
我看了一下 `drop`,`delete_if` 等方法。我试着用 `each_with_index` 做了这个操作:
a = ["a", "b", "c"]
b = a.delete_if {|x| x.index > 1 }
我看了一下 `drop`,`delete_if` 等方法。我试着用 `each_with_index` 做了这个操作:
new_arr = []
a.each_with_index do |obj, index|
if index > 1
obj.delete
end
new_arry << obj
end
如果数组元素的位置大于一定值,如何删除该元素?