summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/mrblib/array.rb
diff options
context:
space:
mode:
authorJun Hiroe <[email protected]>2014-04-18 22:28:42 +0900
committerJun Hiroe <[email protected]>2014-04-18 23:13:10 +0900
commitcb5e6edb7f8fa1117192da7732446229d93993c8 (patch)
tree3088dd763041945a8ecbee0b86289acea8e27a13 /mrbgems/mruby-array-ext/mrblib/array.rb
parent80646a7890a0e786f7f6f1614a519eccae3b0341 (diff)
downloadmruby-cb5e6edb7f8fa1117192da7732446229d93993c8.tar.gz
mruby-cb5e6edb7f8fa1117192da7732446229d93993c8.zip
Add a block argument with Array#uniq
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb14
1 files changed, 11 insertions, 3 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index 0da5d05a8..6c47235fe 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -43,16 +43,24 @@ class Array
##
# call-seq:
- # ary.uniq -> new_ary
+ # ary.uniq -> new_ary
+ # ary.uniq { |item| ... } -> new_ary
#
# Returns a new array by removing duplicate values in +self+.
#
# a = [ "a", "a", "b", "b", "c" ]
# a.uniq #=> ["a", "b", "c"]
#
- def uniq
+ # b = [["student","sam"], ["student","george"], ["teacher","matz"]]
+ # b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
+ #
+ def uniq(&block)
ary = self.dup
- ary.uniq!
+ if block
+ ary.uniq!(&block)
+ else
+ ary.uniq!
+ end
ary
end