Commit b27d665d authored by mirichi's avatar mirichi

Merge remote-tracking branch 'remotes/mruby/master' into enhance

parents e6c757b5 f2204ee5
...@@ -490,4 +490,23 @@ class Array ...@@ -490,4 +490,23 @@ class Array
self self
end end
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 end
...@@ -197,3 +197,12 @@ assert("Array#reject!") do ...@@ -197,3 +197,12 @@ assert("Array#reject!") do
assert_equal [1, 2, 3], a.reject! { |val| val > 3 } assert_equal [1, 2, 3], a.reject! { |val| val > 3 }
assert_equal [1, 2, 3], a assert_equal [1, 2, 3], a
end 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
...@@ -14,3 +14,19 @@ assert('regression for #1572') do ...@@ -14,3 +14,19 @@ assert('regression for #1572') do
o = `bin/mruby -b #{bin.path}`.strip o = `bin/mruby -b #{bin.path}`.strip
assert_equal o, '"ok"' assert_equal o, '"ok"'
end 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment