summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/mrblib/array.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-04-23 00:25:50 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-04-23 00:25:50 +0900
commit68027317b0401f621de9d89676160522b3238b07 (patch)
tree1c220a5c8c6fa94af1d615ac1b16a081e130e0ac /mrbgems/mruby-array-ext/mrblib/array.rb
parenta48e0018fd8065d23f67fe354226ca346728ee87 (diff)
downloadmruby-68027317b0401f621de9d89676160522b3238b07.tar.gz
mruby-68027317b0401f621de9d89676160522b3238b07.zip
Array#insert: simpler (and faster) implementation; ref #2107
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb14
1 files changed, 3 insertions, 11 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index df7635e8f..49d0db0d5 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -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