From ced640951b0e7164a12ea1770330eba3e6109fc2 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 17 Jun 2019 11:33:44 -0700 Subject: Implement Array#minmax Array#minmax was previous not implemented, so calling #minmax on array was actually calling Enumerable#minmax. This is a simple implementation of #minmax by just calling rb_ary_min and rb_ary_max, which improves performance significantly. Fixes [Bug #15929] --- array.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'array.c') diff --git a/array.c b/array.c index d441de84e4..31f078610d 100644 --- a/array.c +++ b/array.c @@ -4850,6 +4850,26 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary) return result; } +/* + * call-seq: + * ary.minmax -> [obj, obj] + * ary.minmax {| a,b | block } -> [obj, obj] + * + * Returns a two element array which contains the minimum and the + * maximum value in the array. + * + * Can be given an optional block to override the default comparison + * method a <=> b. + */ +static VALUE +rb_ary_minmax(VALUE ary) +{ + if (rb_block_given_p()) { + return rb_call_super(0, NULL); + } + return rb_assoc_new(rb_ary_min(0, 0, ary), rb_ary_max(0, 0, ary)); +} + static int push_value(st_data_t key, st_data_t val, st_data_t ary) { @@ -6902,6 +6922,7 @@ Init_Array(void) rb_define_method(rb_cArray, "max", rb_ary_max, -1); rb_define_method(rb_cArray, "min", rb_ary_min, -1); + rb_define_method(rb_cArray, "minmax", rb_ary_minmax, 0); rb_define_method(rb_cArray, "uniq", rb_ary_uniq, 0); rb_define_method(rb_cArray, "uniq!", rb_ary_uniq_bang, 0); -- cgit v1.2.3