mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Array#insert: simpler (and faster) implementation; ref #2107
This commit is contained in:
@@ -504,17 +504,9 @@ class Array
|
||||
# a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
|
||||
# a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
|
||||
|
||||
def insert(idx, *obj)
|
||||
def insert(idx, *args)
|
||||
idx += self.size + 1 if idx < 0
|
||||
ary = []
|
||||
before_ary = self[0, idx]
|
||||
after_ary = self[idx, self.size]
|
||||
before_ary.each {|val| ary << val} unless before_ary == nil
|
||||
while ary.size < idx
|
||||
ary << nil
|
||||
end
|
||||
obj.each {|val| ary << val} unless obj == nil
|
||||
after_ary.each {|val| ary << val} unless after_ary == nil
|
||||
self.replace(ary)
|
||||
self[idx, 0] = args
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user