summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-08-30 22:38:15 +0900
committerGitHub <[email protected]>2019-08-30 22:38:15 +0900
commit55ef4e4bdda01153c93cca26313d0329f765b591 (patch)
tree5250588067557679e15090512dc31593f0df7830
parent0ce1878abafcaa84e373de144480aaadfff3039b (diff)
parent28d1f6cdc48782247b6d39be0e1a794404da26f5 (diff)
downloadmruby-55ef4e4bdda01153c93cca26313d0329f765b591.tar.gz
mruby-55ef4e4bdda01153c93cca26313d0329f765b591.zip
Merge pull request #4677 from shuujii/array-permutation-combination-without-block-should-return-self
`Array#(permutation|combination)` without block should return `self`
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb14
-rw-r--r--mrbgems/mruby-array-ext/test/array.rb7
2 files changed, 12 insertions, 9 deletions
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<size
result = [self[i]]
@@ -835,6 +834,7 @@ class Array
i += 1
end
end
+ self
end
##
@@ -861,9 +861,8 @@ class Array
# a.combination(5).to_a #=> [] # 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<size
result = [self[i]]
@@ -882,6 +881,7 @@ class Array
i += 1
end
end
+ self
end
##
diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb
index 4fad42518..27075747e 100644
--- a/mrbgems/mruby-array-ext/test/array.rb
+++ b/mrbgems/mruby-array-ext/test/array.rb
@@ -3,8 +3,11 @@
def assert_permutation_combination(exp, receiver, meth, *args)
act = []
- receiver.__send__(meth, *args) { |v| act << v }
- assert_equal(exp, act.sort)
+ ret = receiver.__send__(meth, *args) { |v| act << v }
+ assert "assert_#{meth}" do
+ assert_equal(exp, act.sort)
+ assert_same(receiver, ret)
+ end
end
def assert_permutation(exp, receiver, *args)