mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Add Array#difference method from Ruby2.6.
This commit is contained in:
@@ -124,6 +124,22 @@ class Array
|
||||
ary
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# ary.difference(other_ary1, other_ary2, ...) -> new_ary
|
||||
#
|
||||
# Returns a new array that is a copy of the original array, removing all
|
||||
# occurences of any item that also appear in +other_ary+. The order is
|
||||
# preserved from the original array.
|
||||
#
|
||||
def difference(*args)
|
||||
ary = self.dup
|
||||
args.each do |x|
|
||||
ary = self - x
|
||||
end
|
||||
ary
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# ary & other_ary -> new_ary
|
||||
|
||||
@@ -93,6 +93,14 @@ assert("Array#union") do
|
||||
assert_equal [1, 2, 3, 4, 5], a.union(b,c)
|
||||
end
|
||||
|
||||
assert("Array#difference") do
|
||||
a = [1, 2, 3, 1]
|
||||
b = [1, 4]
|
||||
c = [1, 5]
|
||||
|
||||
assert_equal [2, 3], a.difference(b,c)
|
||||
end
|
||||
|
||||
assert("Array#&") do
|
||||
a = [1, 2, 3, 1]
|
||||
b = [1, 4]
|
||||
|
||||
Reference in New Issue
Block a user