diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-09-21 00:01:48 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-09-21 00:01:48 +0900 |
| commit | f23f2bbdad7a15bec8812b029cb23c2117d7c63c (patch) | |
| tree | 8e310268b491f6b59673e36a94be4bbd30b778f7 /mrbgems/mruby-array-ext/mrblib/array.rb | |
| parent | dd346be99aaf6d0220f067c78decb29548c7dfe6 (diff) | |
| download | mruby-f23f2bbdad7a15bec8812b029cb23c2117d7c63c.tar.gz mruby-f23f2bbdad7a15bec8812b029cb23c2117d7c63c.zip | |
Implement `Array#union` which is introduced in Ruby2.6.
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 4f676a121..8c2acc7ac 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -139,6 +139,25 @@ class Array ## # call-seq: + # ary.union(other_ary,...) -> new_ary + # + # Set Union---Returns a new array by joining this array with + # <i>other_ary</i>, removing duplicates. + # + # ["a", "b", "c"].union(["c", "d", "a"], ["a", "c", "e"]) + # #=> ["a", "b", "c", "d", "e"] + # + def union(*args) + ary = self.dup + args.each_with_index do |x,i| + ary.concat(x) + ary.uniq! if i % 20 == 0 + end + ary.uniq! or ary + end + + ## + # call-seq: # ary & other_ary -> new_ary # # Set Intersection---Returns a new array |
