@@ -103,7 +103,7 @@ static VALUE sym_auto, sym_left, sym_right, sym_sturges;
103103
104104static VALUE cHistogram ;
105105
106- static VALUE orig_enum_sum , orig_ary_sum ;
106+ static ID id_builtin_sum ;
107107
108108inline static VALUE
109109f_add (VALUE x , VALUE y )
@@ -800,7 +800,7 @@ ary_calculate_sum(VALUE ary, VALUE init, int skip_na, long *na_count_out)
800800 * [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm)
801801 * to compensate the result precision when the `ary` includes Float values.
802802 *
803- * Note that This library does not redefine `sum` method introduced in Ruby 2.4.
803+ * Redefines `sum` ( Ruby >= 2.4). Original is aliased as `__sum__` .
804804 *
805805 * @return [Number] A summation value
806806 */
@@ -817,7 +817,7 @@ ary_sum(int argc, VALUE* argv, VALUE ary)
817817
818818#ifndef HAVE_ENUM_SUM
819819 if (!skip_na ) {
820- return rb_funcall (orig_ary_sum , rb_intern ( "call" ) , argc , & v );
820+ return rb_funcall (ary , id_builtin_sum , argc , & v );
821821 }
822822#endif
823823
@@ -1263,7 +1263,7 @@ enum_sum_count(VALUE obj, VALUE init, int skip_na, VALUE *sum_ptr, long *count_p
12631263 * [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm)
12641264 * to compensate the result precision when the `enum` includes Float values.
12651265 *
1266- * Note that This library does not redefine `sum` method introduced in Ruby 2.4.
1266+ * Redefines `sum` ( Ruby >= 2.4). Original is aliased as `__sum__` .
12671267 *
12681268 * @return [Number] A summation value
12691269 */
@@ -1283,7 +1283,7 @@ enum_sum(int argc, VALUE* argv, VALUE obj)
12831283 enum_sum_count (obj , init , skip_na , & sum , NULL );
12841284 }
12851285 else {
1286- rb_funcall (orig_enum_sum , rb_intern ( "call" ) , argc , & init );
1286+ sum = rb_funcall (obj , id_builtin_sum , argc , & init );
12871287 }
12881288#else
12891289 enum_sum_count (obj , init , skip_na , & sum , NULL );
@@ -2530,9 +2530,9 @@ Init_extension(void)
25302530
25312531 mEnumerableStatistics = rb_const_get_at (rb_cObject , rb_intern ("EnumerableStatistics" ));
25322532
2533- orig_enum_sum = rb_funcall (rb_mEnumerable , rb_intern ("public_instance_method" ), 1 , rb_str_new_cstr ("sum" ));
2534- orig_ary_sum = rb_funcall (rb_cArray , rb_intern ("public_instance_method" ), 1 , rb_str_new_cstr ("sum" ));
2533+ id_builtin_sum = rb_intern ("__sum__" );
25352534
2535+ rb_define_alias (rb_mEnumerable , "__sum__" , "sum" );
25362536 rb_define_method (rb_mEnumerable , "sum" , enum_sum , -1 );
25372537 rb_define_method (rb_mEnumerable , "mean_variance" , enum_mean_variance_m , -1 );
25382538 rb_define_method (rb_mEnumerable , "mean" , enum_mean , 0 );
@@ -2541,6 +2541,7 @@ Init_extension(void)
25412541 rb_define_method (rb_mEnumerable , "stdev" , enum_stdev , -1 );
25422542 rb_define_method (rb_mEnumerable , "value_counts" , enum_value_counts , -1 );
25432543
2544+ rb_define_alias (rb_cArray , "__sum__" , "sum" );
25442545 rb_define_method (rb_cArray , "sum" , ary_sum , -1 );
25452546 rb_define_method (rb_cArray , "mean_variance" , ary_mean_variance_m , -1 );
25462547 rb_define_method (rb_cArray , "mean" , ary_mean , -1 );
0 commit comments