From 5adef8ba44b92ca01692451e21af5c3f47e8853c Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sat, 6 Apr 2019 17:40:51 +0900 Subject: Move `Array#(append|prepend)` from core to `mruby-ary-ext` They are not included in ISO standard. --- mrbgems/mruby-array-ext/mrblib/array.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 387bd6c90..59b6087d2 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -936,4 +936,7 @@ class Array end h end + + alias append push + alias prepend unshift end -- cgit v1.2.3 From 778310260ca51b91125bd6deeb836c5032fa837e Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 26 Jul 2019 11:48:53 +0900 Subject: Drop dependency from `mruby-array-ext` to `mruby-enum-ext` --- mrbgems/mruby-array-ext/mrbgem.rake | 1 - mrbgems/mruby-array-ext/mrblib/array.rb | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrbgem.rake b/mrbgems/mruby-array-ext/mrbgem.rake index 58d4428d4..882caf1ab 100644 --- a/mrbgems/mruby-array-ext/mrbgem.rake +++ b/mrbgems/mruby-array-ext/mrbgem.rake @@ -2,5 +2,4 @@ MRuby::Gem::Specification.new('mruby-array-ext') do |spec| spec.license = 'MIT' spec.author = 'mruby developers' spec.summary = 'Array class extension' - spec.add_test_dependency 'mruby-enumerator', core: 'mruby-enumerator' end diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 59b6087d2..1cd1eb643 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -903,8 +903,8 @@ class Array column_count = nil self.each do |row| raise TypeError unless row.is_a?(Array) - column_count ||= row.count - raise IndexError, 'element size differs' unless column_count == row.count + column_count ||= row.size + raise IndexError, 'element size differs' unless column_count == row.size end Array.new(column_count) do |column_index| -- cgit v1.2.3 From 28d1f6cdc48782247b6d39be0e1a794404da26f5 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 30 Aug 2019 17:04:25 +0900 Subject: `Array#(permutation|combination)` without block should return `self` --- mrbgems/mruby-array-ext/mrblib/array.rb | 14 +++++++------- mrbgems/mruby-array-ext/test/array.rb | 7 +++++-- 2 files changed, 12 insertions(+), 9 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 1cd1eb643..fef78f83f 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -815,12 +815,11 @@ class Array # a.permutation(0).to_a #=> [[]] # one permutation of length 0 # a.permutation(4).to_a #=> [] # no permutations of length 4 def permutation(n=self.size, &block) - size = self.size return to_enum(:permutation, n) unless block - return if n > size + size = self.size if n == 0 - yield [] - else + yield [] + elsif n <= size i = 0 while i [] # no combinations of length 5 def combination(n, &block) - size = self.size return to_enum(:combination, n) unless block - return if n > size + size = self.size if n == 0 yield [] elsif n == 1 @@ -872,7 +871,7 @@ class Array yield [self[i]] i += 1 end - else + elsif n <= size i = 0 while i Date: Sat, 31 Aug 2019 13:03:30 +0900 Subject: `Array#permutation` with a negative argument should not yield Before this patch: $ bin/mruby -e '[1].permutation(-1){|v| p v}' #=> [1] After this patch (same as Ruby): $ bin/mruby -e '[1].permutation(-1){|v| p v}' #=> no output --- mrbgems/mruby-array-ext/mrblib/array.rb | 2 +- mrbgems/mruby-array-ext/test/array.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fef78f83f..fc5d87f2c 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -819,7 +819,7 @@ class Array size = self.size if n == 0 yield [] - elsif n <= size + elsif 0 < n && n <= size i = 0 while i Date: Mon, 16 Sep 2019 07:52:19 +0900 Subject: Add `Array#difference` method from Ruby2.6. --- mrbgems/mruby-array-ext/mrblib/array.rb | 16 ++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 ++++++++ 2 files changed, 24 insertions(+) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fc5d87f2c..f9f232321 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -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 diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index a4e328b71..029325aab 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -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] -- cgit v1.2.3 From 57a0132b41920ea14b903113586bf970c8210efa Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 16 Sep 2019 07:55:18 +0900 Subject: Add `filter` aliases for `Enumerable` and `Hash`. --- mrbgems/mruby-array-ext/mrblib/array.rb | 1 + mrbgems/mruby-enum-ext/mrblib/enum.rb | 1 + mrbgems/mruby-hash-ext/mrblib/hash.rb | 3 +++ 3 files changed, 5 insertions(+) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index f9f232321..5201cbe57 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -955,4 +955,5 @@ class Array alias append push alias prepend unshift + alias filter! select! end diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index b427bd67e..171737e28 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -826,4 +826,5 @@ module Enumerable end hash.values end + alias filter select end diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 547f3404a..33e2dcb9f 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -494,4 +494,7 @@ class Hash self.fetch(k, &block) end end + + alias filter select + alias filter! select! end -- cgit v1.2.3 From 1fb4e73981d2c9588eb25acfcc2d34fca3a415bd Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 20 Sep 2019 09:28:56 +0900 Subject: Fix typo in `Array#difference` document [ci skip] --- mrbgems/mruby-array-ext/mrblib/array.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 5201cbe57..7cec44dba 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -129,7 +129,7 @@ class Array # 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 + # occurrences of any item that also appear in +other_ary+. The order is # preserved from the original array. # def difference(*args) -- cgit v1.2.3 From 67ea80b2bd2df64bbfefd0ba8f74f6eeb52074aa Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:38:25 +0900 Subject: Fixed a bug in `Array#difference`. --- mrbgems/mruby-array-ext/mrblib/array.rb | 4 ++-- mrbgems/mruby-array-ext/test/array.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 7cec44dba..d4a72a927 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -133,9 +133,9 @@ class Array # preserved from the original array. # def difference(*args) - ary = self.dup + ary = self args.each do |x| - ary = self - x + ary = ary - x end ary end diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 029325aab..0cfb1e857 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -94,9 +94,9 @@ assert("Array#union") do end assert("Array#difference") do - a = [1, 2, 3, 1] - b = [1, 4] - c = [1, 5] + a = [1, 2, 3, 1, 6, 7] + b = [1, 4, 6] + c = [1, 5, 7] assert_equal [2, 3], a.difference(b,c) end -- cgit v1.2.3 From 682406a38425fd6389a67580e524b9c5c15c25fb Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:39:04 +0900 Subject: Move `Array#difference` just after `Array#-`. --- mrbgems/mruby-array-ext/mrblib/array.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index d4a72a927..fa3db3a70 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -88,6 +88,22 @@ class Array array 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 + # occurrences of any item that also appear in +other_ary+. The order is + # preserved from the original array. + # + def difference(*args) + ary = self + args.each do |x| + ary = ary - x + end + ary + end + ## # call-seq: # ary | other_ary -> new_ary @@ -124,22 +140,6 @@ 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 - # occurrences of any item that also appear in +other_ary+. The order is - # preserved from the original array. - # - def difference(*args) - ary = self - args.each do |x| - ary = ary - x - end - ary - end - ## # call-seq: # ary & other_ary -> new_ary -- cgit v1.2.3 From bdacdfaea9bf4b52770c32d60b7d402b48297c58 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:39:29 +0900 Subject: Add `Array#intersection` which is new in Ruby2.7. --- mrbgems/mruby-array-ext/mrblib/array.rb | 18 ++++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 ++++++++ 2 files changed, 26 insertions(+) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') 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 @@ -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 other_ary, 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 diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 0cfb1e857..cb76559c7 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -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 -- cgit v1.2.3 From ac29638a8ac5ba25d4004c06e25074d1c1503a99 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Mon, 14 Oct 2019 18:16:57 +0900 Subject: Fix the example of `Array#intersection` in the document [ci skip] --- mrbgems/mruby-array-ext/mrblib/array.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index be1676666..5492ba2eb 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -178,10 +178,10 @@ class Array # ary.intersection(other_ary,...) -> new_ary # # Set Intersection---Returns a new array containing elements common to - # this array and other_ary, removing duplicates. + # this array and other_arys, removing duplicates. The order is + # preserved from the original array. # - # ["a", "b", "c"].union(["c", "d", "a"], ["a", "c", "e"]) - # #=> ["a", "b", "c", "d", "e"] + # [1, 2, 3].intersection([3, 4, 1], [1, 3, 5]) #=> [1, 3] # def intersection(*args) ary = self -- cgit v1.2.3