Skip to content

Commit 6cf5156

Browse files
kojix2kou
andauthored
Fix "method redefined" warnings for sum method (#46)
Fixes #43 - Create an alias method `__sum__` for Enumerable and Array. - Call the alias method internally. - Update the documentation. --------- Co-authored-by: Sutou Kouhei <[email protected]>
1 parent e03d0f5 commit 6cf5156

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ require 'enumerable/statistics'
3030

3131
The following methods are supplied by this library:
3232

33+
- `Array#sum`, `Enumerable#sum`
34+
- Calculates a sum of values in an array or an enumerable
35+
- Supports `skip_na: true` to skip `nil` and `NaN` values
3336
- `Array#mean`, `Enumerable#mean`
3437
- Calculates a mean of values in an array or an enumerable
3538
- `Array#variance`, `Enumerable#variance`

ext/enumerable/statistics/extension/statistics.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static VALUE sym_auto, sym_left, sym_right, sym_sturges;
103103

104104
static VALUE cHistogram;
105105

106-
static VALUE orig_enum_sum, orig_ary_sum;
106+
static ID id_builtin_sum;
107107

108108
inline static VALUE
109109
f_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

Comments
 (0)