summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormirichi <[email protected]>2014-04-23 19:03:35 +0900
committermirichi <[email protected]>2014-04-23 19:03:35 +0900
commitb27d665d2d80b71f3c88aee75fa4037e5a469998 (patch)
treef3a39c6f8078b72e0f7a93a2dd3a48a41e0ab69c
parente6c757b5c190429832c057ea5ecea3ed33484276 (diff)
parentf2204ee524810f3598d97b7b8c184af98d2f94d9 (diff)
downloadmruby-b27d665d2d80b71f3c88aee75fa4037e5a469998.tar.gz
mruby-b27d665d2d80b71f3c88aee75fa4037e5a469998.zip
Merge remote-tracking branch 'remotes/mruby/master' into enhance
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb19
-rw-r--r--mrbgems/mruby-array-ext/test/array.rb9
-rw-r--r--mrbgems/mruby-bin-mruby/bintest/mruby.rb16
3 files changed, 44 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index 78d9ff78a..49d0db0d5 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -490,4 +490,23 @@ 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, *args)
+ idx += self.size + 1 if idx < 0
+ self[idx, 0] = args
+ self
+ end
end
diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb
index 0b425281f..d15ea2a64 100644
--- a/mrbgems/mruby-array-ext/test/array.rb
+++ b/mrbgems/mruby-array-ext/test/array.rb
@@ -197,3 +197,12 @@ assert("Array#reject!") do
assert_equal [1, 2, 3], a.reject! { |val| val > 3 }
assert_equal [1, 2, 3], a
end
+
+assert("Array#insert") do
+ a = ["a", "b", "c", "d"]
+ assert_equal ["a", "b", 99, "c", "d"], a.insert(2, 99)
+ assert_equal ["a", "b", 99, "c", 1, 2, 3, "d"], a.insert(-2, 1, 2, 3)
+
+ b = ["a", "b", "c", "d"]
+ assert_equal ["a", "b", "c", "d", nil, nil, 99], b.insert(6, 99)
+end
diff --git a/mrbgems/mruby-bin-mruby/bintest/mruby.rb b/mrbgems/mruby-bin-mruby/bintest/mruby.rb
index 22872c389..a453400fc 100644
--- a/mrbgems/mruby-bin-mruby/bintest/mruby.rb
+++ b/mrbgems/mruby-bin-mruby/bintest/mruby.rb
@@ -14,3 +14,19 @@ assert('regression for #1572') do
o = `bin/mruby -b #{bin.path}`.strip
assert_equal o, '"ok"'
end
+
+assert '$0 value' do
+ script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb')
+
+ # .rb script
+ script.write "p $0\n"
+ script.flush
+ assert_equal "\"#{script.path}\"", `./bin/mruby "#{script.path}"`.chomp
+
+ # .mrb file
+ `./bin/mrbc -o "#{bin.path}" "#{script.path}"`
+ assert_equal "\"#{bin.path}\"", `./bin/mruby -b "#{bin.path}"`.chomp
+
+ # one liner
+ assert_equal '"-e"', `./bin/mruby -e 'p $0'`.chomp
+end