summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2013-03-22 18:33:48 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2013-03-22 18:33:48 -0700
commit6804ab3ab88763032f2b0c3ddd12985f2d1a73c6 (patch)
tree8518143b48495a3eb320d57f83af95b7c4e0ce89 /mrbgems/mruby-array-ext
parent1ed2afeeca18c059a14ed4e97d747476eb625d53 (diff)
parent0f61d1d6b7a580262ab2f7ce7284e387079a0c00 (diff)
downloadmruby-6804ab3ab88763032f2b0c3ddd12985f2d1a73c6.tar.gz
mruby-6804ab3ab88763032f2b0c3ddd12985f2d1a73c6.zip
Merge pull request #1044 from kouki-o-iij/pr-array-ext
add method(uniq, -, |, &, flatten, compact) of Array to mruby-array-ext
Diffstat (limited to 'mrbgems/mruby-array-ext')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb73
-rw-r--r--mrbgems/mruby-array-ext/test/array.rb79
2 files changed, 152 insertions, 0 deletions
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