diff --git a/array.c b/array.c index 14be7776db..dca4464c59 100644 --- a/array.c +++ b/array.c @@ -7103,56 +7103,45 @@ rb_ary_combination_size(VALUE ary, VALUE args, VALUE eobj) /* * call-seq: - * array.combination(n) {|element| ... } -> self - * array.combination(n) -> new_enumerator + * combination(n) {|element| ... } -> self + * combination(n) -> new_enumerator * - * Calls the block, if given, with combinations of elements of +self+; - * returns +self+. The order of combinations is indeterminate. + * When a block and a positive + * {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects] + * argument +n+ (0 < n <= self.size) + * are given, calls the block with all +n+-tuple combinations of +self+; + * returns +self+: * - * When a block and an in-range positive Integer argument +n+ (0 < n <= self.size) - * are given, calls the block with all +n+-tuple combinations of +self+. - * - * Example: - * - * a = [0, 1, 2] - * a.combination(2) {|combination| p combination } + * a = %w[a b c] # => ["a", "b", "c"] + * a.combination(2) {|combination| p combination } # => ["a", "b", "c"] * * Output: * - * [0, 1] - * [0, 2] - * [1, 2] + * ["a", "b"] + * ["a", "c"] + * ["b", "c"] * - * Another example: + * The order of the yielded combinations is not guaranteed. * - * a = [0, 1, 2] - * a.combination(3) {|combination| p combination } + * When +n+ is zero, calls the block once with a new empty array: * - * Output: - * - * [0, 1, 2] - * - * When +n+ is zero, calls the block once with a new empty +Array+: - * - * a = [0, 1, 2] - * a1 = a.combination(0) {|combination| p combination } + * a.combination(0) {|combination| p combination } + * [].combination(0) {|combination| p combination } * * Output: * * [] + * [] * - * When +n+ is out of range (negative or larger than self.size), + * When +n+ is negative or larger than +self.size+ and +self+ is non-empty, * does not call the block: * - * a = [0, 1, 2] - * a.combination(-1) {|combination| fail 'Cannot happen' } - * a.combination(4) {|combination| fail 'Cannot happen' } + * a.combination(-1) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"] + * a.combination(4) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"] * - * Returns a new Enumerator if no block given: - * - * a = [0, 1, 2] - * a.combination(2) # => # + * With no block given, returns a new Enumerator. * + * Related: Array#permutation. */ static VALUE