add method(uniq, -, |, &, flatten, compact) and test of Array to mruby-array-ext
authorKouki Ooyatsu <[email protected]>
Fri, 22 Mar 2013 07:39:48 +0000 (22 16:39 +0900)
committerKouki Ooyatsu <[email protected]>
Fri, 22 Mar 2013 07:45:12 +0000 (22 16:45 +0900)
mrbgems/mruby-array-ext/mrblib/array.rb [new file with mode: 0644]
mrbgems/mruby-array-ext/test/array.rb

diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
new file mode 100644 (file)
index 0000000..83eaaa3
--- /dev/null
@@ -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
index 36da6bb..1c441ce 100644 (file)
@@ -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