From: Kouki Ooyatsu Date: Fri, 22 Mar 2013 07:39:48 +0000 (+0900) Subject: add method(uniq, -, |, &, flatten, compact) and test of Array to mruby-array-ext X-Git-Tag: 1.0.0~669^2 X-Git-Url: https://repo.or.cz/mruby.git/commitdiff_plain/0f61d1d6b7a580262ab2f7ce7284e387079a0c00 add method(uniq, -, |, &, flatten, compact) and test of Array to mruby-array-ext --- diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb new file mode 100644 index 000000000..83eaaa35e --- /dev/null +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -0,0 +1,73 @@ +class Array + def uniq! + ary = self.dup + result = [] + while ary.size > 0 + result << ary.shift + ary.delete(result.last) + end + self.replace(result) + end + + def uniq + self.dup.uniq! + end + + def -(elem) + raise TypeError, "can't convert to Array" unless elem.class == Array + + hash = {} + array = [] + elem.each { |x| hash[x] = true } + self.each { |x| array << x unless hash[x] } + array + end + + def |(elem) + raise TypeError, "can't convert to Array" unless elem.class == Array + + (self + elem).uniq! + end + + def &(elem) + raise TypeError, "can't convert to Array" unless elem.class == Array + + hash = {} + array = [] + elem.each{|v| hash[v] = true } + self.each do |v| + if hash[v] + array << v + hash.delete v + end + end + array + end + + def flatten(depth=nil) + ar = [] + self.each do |e| + if e.is_a?(Array) && (depth.nil? || depth > 0) + ar += e.flatten(depth.nil? ? nil : depth - 1) + else + ar << e + end + end + ar + end + + def flatten! + self.replace(self.flatten) + end + + def compact + result = self.dup + result.compact! + result + end + + def compact! + result = self.select { |e| e != nil } + self.replace(result) + end +end diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 36da6bb17..1c441cec4 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -28,3 +28,82 @@ assert("Array#rassoc") do a.rassoc("four").nil? end +assert("Array#uniq!") do + a = [1, 2, 3, 1] + a.uniq! + a == [1, 2, 3] +end + +assert("Array#uniq") do + a = [1, 2, 3, 1] + a.uniq == [1, 2, 3] && a == [1, 2, 3, 1] +end + +assert("Array#-") do + a = [1, 2, 3, 1] + b = [1] + c = 1 + e1 = nil + + begin + a - c + rescue => e1 + end + + (a - b) == [2, 3] and e1.class == TypeError and a == [1, 2, 3, 1] +end + +assert("Array#|") do + a = [1, 2, 3, 1] + b = [1, 4] + c = 1 + e1 = nil + + begin + a | c + rescue => e1 + end + + (a | b) == [1, 2, 3, 4] and e1.class == TypeError and a == [1, 2, 3, 1] +end + +assert("Array#&") do + a = [1, 2, 3, 1] + b = [1, 4] + c = 1 + e1 = nil + + begin + a & c + rescue => e1 + end + + (a & b) == [1] and e1.class == TypeError and a == [1, 2, 3, 1] +end + +assert("Array#flatten") do + [1, 2, "3", {4=>5}, :'6'] == [1, 2, "3", {4=>5}, :'6'].flatten and + [1, 2, 3, 4, 5, 6] == [1, 2, [3, 4, 5], 6].flatten and + [1, 2, 3, 4, 5, 6] == [1, 2, [3, [4, 5], 6]].flatten and + [1, [2, [3, [4, [5, [6]]]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(0) and + [1, 2, [3, [4, [5, [6]]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(1) and + [1, 2, 3, [4, [5, [6]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(2) and + [1, 2, 3, 4, [5, [6]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(3) and + [1, 2, 3, 4, 5, [6]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(4) and + [1, 2, 3, 4, 5, 6] == [1, [2, [3, [4, [5, [6]]]]]].flatten(5) +end + +assert("Array#flatten!") do + [1, 2, 3, 4, 5, 6] == [1, 2, [3, [4, 5], 6]].flatten! +end + +assert("Array#compact") do + a = [1, nil, "2", nil, :t, false, nil] + a.compact == [1, "2", :t, false] && a == [1, nil, "2", nil, :t, false, nil] +end + +assert("Array#compact!") do + a = [1, nil, "2", nil, :t, false, nil] + a.compact! + a == [1, "2", :t, false] +end