aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-17 14:09:37 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-17 14:09:37 +0000
commit808be5f96b17246341c0ee7a32e2bbae3e8dc13f (patch)
tree18a2449cc49d1c564f38e9ae965948b970a5da44
parenteb4208cf38c45d9e1ea00b072e65313d803cd816 (diff)
downloadruby-808be5f96b17246341c0ee7a32e2bbae3e8dc13f.tar.gz
Write document of Enumerable#sum
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55033 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--enum.c26
2 files changed, 30 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index bb68d89..1aa6a39 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Tue May 17 23:08:00 2016 Kenta Murata <mrkn@mrkn.jp>
+
+ * enum.c (enum_sum): [DOC] Write documentation.
+
Tue May 17 22:53:00 2016 Kenta Murata <mrkn@mrkn.jp>
* enum.c (enum_sum): Implement Enumerable#sum.
diff --git a/enum.c b/enum.c
index 157b615..56d304c 100644
--- a/enum.c
+++ b/enum.c
@@ -3668,6 +3668,32 @@ enum_sum_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
/*
+ * call-seq:
+ * enum.sum(init=0) -> number
+ * enum.sum(init=0) {|e| expr } -> number
+ *
+ * Returns the sum of elements in an Enumerable.
+ *
+ * If a block is given, the block is applied to each element
+ * before addition.
+ *
+ * If <i>enum</i> is empty, it returns <i>init</i>.
+ *
+ * For example:
+ *
+ * { 1 => 10, 2 => 20 }.sum {|k, v| k * v } #=> 50
+ * (1..10).sum #=> 55
+ * (1..10).sum {|v| v * 2 } #=> 110
+ * [Object.new].each.sum #=> TypeError
+ *
+ * This method can be used for non-numeric objects by
+ * explicit <i>init</i> argument.
+ *
+ * { 1 => 10, 2 => 20 }.sum([]) #=> [1, 10, 2, 20]
+ * "a\nb\nc".each_line.lazy.map(&:chomp).sum("") #=> "abc"
+ *
+ * Enumerable#sum method may not respect method redefinition of "+"
+ * methods such as Integer#+.
*/
static VALUE
enum_sum(int argc, VALUE* argv, VALUE obj)