From 18718e065a53fd9725208e1b61c50a1a5e2e231d Mon Sep 17 00:00:00 2001 From: take_cheeze Date: Tue, 22 Apr 2014 19:19:08 +0900 Subject: Add test of $0 value in bin/mruby related to #2103 . --- mrbgems/mruby-bin-mruby/bintest/mruby.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 -- cgit v1.2.3 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 +++++++++++++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 9 +++++++++ 2 files changed, 36 insertions(+) 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 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 -- cgit v1.2.3 From 68027317b0401f621de9d89676160522b3238b07 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 23 Apr 2014 00:25:50 +0900 Subject: Array#insert: simpler (and faster) implementation; ref #2107 --- mrbgems/mruby-array-ext/mrblib/array.rb | 14 +++----------- 1 file 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 -- cgit v1.2.3