summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/mrblib/array.rb
diff options
context:
space:
mode:
authorYukihiro Matz Matsumoto <[email protected]>2013-03-23 10:54:09 +0900
committerYukihiro Matz Matsumoto <[email protected]>2013-03-23 10:54:09 +0900
commit5717994452117f09b6931178fa45a471965b30a8 (patch)
tree18a4fbafba380b6abc2853309cea1492804af3d3 /mrbgems/mruby-array-ext/mrblib/array.rb
parent6804ab3ab88763032f2b0c3ddd12985f2d1a73c6 (diff)
downloadmruby-5717994452117f09b6931178fa45a471965b30a8.tar.gz
mruby-5717994452117f09b6931178fa45a471965b30a8.zip
Array bang methods should return nil if no change happen
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb37
1 files changed, 31 insertions, 6 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index 83eaaa35e..b3ff9bfca 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -6,11 +6,17 @@ class Array
result << ary.shift
ary.delete(result.last)
end
- self.replace(result)
+ if result.size == self.size
+ nil
+ else
+ self.replace(result)
+ end
end
def uniq
- self.dup.uniq!
+ ary = self.dup
+ ary.uniq!
+ ary
end
def -(elem)
@@ -26,7 +32,8 @@ class Array
def |(elem)
raise TypeError, "can't convert to Array" unless elem.class == Array
- (self + elem).uniq!
+ ary = self + elem
+ ary.uniq! or ary
end
def &(elem)
@@ -56,8 +63,22 @@ class Array
ar
end
- def flatten!
- self.replace(self.flatten)
+ def flatten!(depth=nil)
+ modified = false
+ ar = []
+ self.each do |e|
+ if e.is_a?(Array) && (depth.nil? || depth > 0)
+ ar += e.flatten(depth.nil? ? nil : depth - 1)
+ modified = true
+ else
+ ar << e
+ end
+ end
+ if modified
+ self.replace(ar)
+ else
+ nil
+ end
end
def compact
@@ -68,6 +89,10 @@ class Array
def compact!
result = self.select { |e| e != nil }
- self.replace(result)
+ if result.size == self.size
+ nil
+ else
+ self.replace(result)
+ end
end
end