mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Add Array#intersection which is new in Ruby2.7.
This commit is contained in:
@@ -173,6 +173,24 @@ class Array
|
||||
array
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# ary.intersection(other_ary,...) -> new_ary
|
||||
#
|
||||
# Set Intersection---Returns a new array containing elements common to
|
||||
# this array and <i>other_ary</i>, removing duplicates.
|
||||
#
|
||||
# ["a", "b", "c"].union(["c", "d", "a"], ["a", "c", "e"])
|
||||
# #=> ["a", "b", "c", "d", "e"]
|
||||
#
|
||||
def intersection(*args)
|
||||
ary = self
|
||||
args.each do |x|
|
||||
ary = ary & x
|
||||
end
|
||||
ary
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# ary.flatten -> new_ary
|
||||
|
||||
@@ -111,6 +111,14 @@ assert("Array#&") do
|
||||
assert_equal [1, 2, 3, 1], a
|
||||
end
|
||||
|
||||
assert("Array#intersection") do
|
||||
a = [1, 2, 3, 1, 8, 6, 7, 8]
|
||||
b = [1, 4, 6, 8]
|
||||
c = [1, 5, 7, 8]
|
||||
|
||||
assert_equal [1, 8], a.intersection(b,c)
|
||||
end
|
||||
|
||||
assert("Array#flatten") do
|
||||
assert_equal [1, 2, "3", {4=>5}, :'6'], [1, 2, "3", {4=>5}, :'6'].flatten
|
||||
assert_equal [1, 2, 3, 4, 5, 6], [1, 2, [3, 4, 5], 6].flatten
|
||||
|
||||
Reference in New Issue
Block a user