From a48e0018fd8065d23f67fe354226ca346728ee87 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Mon, 21 Apr 2014 22:14:04 +0900 Subject: Add Array#insert --- mrbgems/mruby-array-ext/mrblib/array.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 78d9ff78a..df7635e8f 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -490,4 +490,31 @@ class Array self end end + + ## + # call-seq: + # ary.insert(index, obj...) -> ary + # + # Inserts the given values before the element with the given +index+. + # + # Negative indices count backwards from the end of the array, where +-1+ is + # the last element. + # + # a = %w{ a b c d } + # 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) + 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) + end end -- cgit v1.2.3