[前][次][番号順一覧][スレッド一覧]

ruby-changes:42959

From: mrkn <ko1@a...>
Date: Tue, 17 May 2016 23:09:42 +0900 (JST)
Subject: [ruby-changes:42959] mrkn:r55033 (trunk): Write document of Enumerable#sum

mrkn	2016-05-17 23:09:37 +0900 (Tue, 17 May 2016)

  New Revision: 55033

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55033

  Log:
    Write document of Enumerable#sum

  Modified files:
    trunk/ChangeLog
    trunk/enum.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55032)
+++ ChangeLog	(revision 55033)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue May 17 23:08:00 2016  Kenta Murata  <mrkn@m...>
+
+	* enum.c (enum_sum): [DOC] Write documentation.
+
 Tue May 17 22:53:00 2016  Kenta Murata  <mrkn@m...>
 
 	* enum.c (enum_sum): Implement Enumerable#sum.
Index: enum.c
===================================================================
--- enum.c	(revision 55032)
+++ enum.c	(revision 55033)
@@ -3668,6 +3668,32 @@ enum_sum_iter_i(RB_BLOCK_CALL_FUNC_ARGLI https://github.com/ruby/ruby/blob/trunk/enum.c#L3668
 
 
 /*
+ * 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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]