--- oldarray.c 2009-10-31 19:58:22.000000000 -0400
+++ array.c 2009-10-31 19:44:04.000000000 -0400
@@ -43,6 +43,7 @@
}
}
+
# define ARY_SHARED_P(ary) \
(assert(!FL_TEST(ary, ELTS_SHARED) || !FL_TEST(ary, RARRAY_EMBED_FLAG)), \
FL_TEST(ary,ELTS_SHARED)!=0)
@@ -1406,6 +1407,55 @@
/*
* call-seq:
+ * array.sum -> double
+ *
+ * Finds the sum of all the values in self.
+ * All of the values must be numbers,
+ * otherwise, the function raises a TypeError.
+ *
+ * a = [1, 2, 3]
+ * a.sum #=> 6
+ *
+ *
+*/
+
+static VALUE
+rb_ary_sum(VALUE ary)
+{
+ long i;
+ double sum = 0.0;
+
+
+ for (i=0; i double
+ *
+ * Finds the mean of values in self. All values must be numbers,
+ * otherwise, the function raises a TypeError.
+ *
+ * a = [1, 2, 3]
+ * a.mean #=> 2
+ *
+ */
+
+static VALUE
+rb_ary_mean(VALUE ary)
+{
+ return DBL2NUM(NUM2DBL(rb_ary_sum(ary)) / RARRAY_LEN(ary));
+}
+
+/*
+ * call-seq:
* array.each_index {|index| block } -> array
*
* Same as Array#each
, but passes the index of the element
@@ -4151,6 +4201,9 @@
rb_define_method(rb_cArray, "take_while", rb_ary_take_while, 0);
rb_define_method(rb_cArray, "drop", rb_ary_drop, 1);
rb_define_method(rb_cArray, "drop_while", rb_ary_drop_while, 0);
+
+ rb_define_method(rb_cArray, "sum", rb_ary_sum, 0);
+ rb_define_method(rb_cArray, "mean", rb_ary_mean, 0);
id_cmp = rb_intern("<=>");
}