summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/mrblib/array.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-10-12 12:39:29 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2019-10-14 17:04:19 +0900
commitbdacdfaea9bf4b52770c32d60b7d402b48297c58 (patch)
treef8ee993ff370683fcff2ad60c540e167bd99ba4e /mrbgems/mruby-array-ext/mrblib/array.rb
parent682406a38425fd6389a67580e524b9c5c15c25fb (diff)
downloadmruby-bdacdfaea9bf4b52770c32d60b7d402b48297c58.tar.gz
mruby-bdacdfaea9bf4b52770c32d60b7d402b48297c58.zip
Add `Array#intersection` which is new in Ruby2.7.
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index fa3db3a70..be1676666 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -175,6 +175,24 @@ class Array
##
# 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
# ary.flatten(level) -> new_ary
#