From d1b5c1095719a6b55fab4f2cdd9fda8d94a45b41 Mon Sep 17 00:00:00 2001 From: BurdetteLamar Date: Thu, 17 Oct 2024 17:31:28 -0500 Subject: [PATCH] [DOC] Tweaks for Array#sum --- array.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/array.c b/array.c index 48614e1ecd..6d430dfd2e 100644 --- a/array.c +++ b/array.c @@ -7924,40 +7924,41 @@ finish_exact_sum(long n, VALUE r, VALUE v, int z) /* * call-seq: - * array.sum(init = 0) -> object - * array.sum(init = 0) {|element| ... } -> object + * sum(init = 0) -> object + * sum(init = 0) {|element| ... } -> object * - * When no block is given, returns the object equivalent to: + * With no block given, returns the sum of +init+ and all elements of +self+; + * for array +array+ and value +init+, equivalent to: * * sum = init * array.each {|element| sum += element } * sum * - * For example, [e1, e2, e3].sum returns init + e1 + e2 + e3. + * For example, [e0, e1, e2].sum returns init + e0 + e1 + e2. * * Examples: * - * a = [0, 1, 2, 3] - * a.sum # => 6 - * a.sum(100) # => 106 + * [0, 1, 2, 3].sum # => 6 + * [0, 1, 2, 3].sum(100) # => 106 + * ['abc', 'def', 'ghi'].sum('jkl') # => "jklabcdefghi" + * [[:foo, :bar], ['foo', 'bar']].sum([2, 3]) + * # => [2, 3, :foo, :bar, "foo", "bar"] * - * The elements need not be numeric, but must be +-compatible - * with each other and with +init+: + * The +init+ value and elements need not be numeric, but must all be +-compatible: * - * a = ['abc', 'def', 'ghi'] - * a.sum('jkl') # => "jklabcdefghi" + * # Raises TypeError: Array can't be coerced into Integer. + * [[:foo, :bar], ['foo', 'bar']].sum(2) * - * When a block is given, it is called with each element - * and the block's return value (instead of the element itself) is used as the addend: + * With a block given, calls the block with each element of +self+; + * the block's return value (instead of the element itself) is used as the addend: * - * a = ['zero', 1, :two] - * s = a.sum('Coerced and concatenated: ') {|element| element.to_s } - * s # => "Coerced and concatenated: zero1two" + * ['zero', 1, :two].sum('Coerced and concatenated: ') {|element| element.to_s } + * # => "Coerced and concatenated: zero1two" * * Notes: * * - Array#join and Array#flatten may be faster than Array#sum - * for an +Array+ of Strings or an +Array+ of Arrays. + * for an array of strings or an array of arrays. * - Array#sum method may not respect method redefinition of "+" methods such as Integer#+. * */