mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Add a block argument with Array#uniq
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -48,6 +48,9 @@ assert("Array#uniq") do
|
||||
a = [1, 2, 3, 1]
|
||||
assert_equal [1, 2, 3], a.uniq
|
||||
assert_equal [1, 2, 3, 1], a
|
||||
|
||||
b = [["student","sam"], ["student","george"], ["teacher","matz"]]
|
||||
assert_equal [["student", "sam"], ["teacher", "matz"]], b.uniq { |s| s.first }
|
||||
end
|
||||
|
||||
assert("Array#-") do
|
||||
|
||||
Reference in New Issue
Block a user