summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-06-07 22:03:02 -0700
committerJeremy Evans <code@jeremyevans.net>2019-08-14 11:22:07 -0700
commitd5c60214c45bafc1cf2a516f852394986f9c84bb (patch)
treece0ef54a02c893f3bae009cbd3477b082910f5c5 /range.c
parent661927a4c55232bd070992d47670a7d411820111 (diff)
Implement Range#minmax
Range#minmax was previous not implemented, so calling #minmax on range was actually calling Enumerable#minmax. This is a simple implementation of #minmax by just calling range_min and range_max. Fixes [Bug #15867] Fixes [Bug #15807]
Diffstat (limited to 'range.c')
-rw-r--r--range.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/range.c b/range.c
index e8515e0b8ae..69f73c04063 100644
--- a/range.c
+++ b/range.c
@@ -1211,6 +1211,27 @@ range_max(int argc, VALUE *argv, VALUE range)
}
}
+/*
+ * call-seq:
+ * rng.minmax -> [obj, obj]
+ * rng.minmax {| a,b | block } -> [obj, obj]
+ *
+ * Returns a two element array which contains the minimum and the
+ * maximum value in the range.
+ *
+ * Can be given an optional block to override the default comparison
+ * method <code>a <=> b</code>.
+ */
+
+static VALUE
+range_minmax(VALUE range)
+{
+ if (rb_block_given_p()) {
+ return rb_call_super(0, NULL);
+ }
+ return rb_assoc_new(range_min(0, NULL, range), range_max(0, NULL, range));
+}
+
int
rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
{
@@ -1697,6 +1718,7 @@ Init_Range(void)
rb_define_method(rb_cRange, "last", range_last, -1);
rb_define_method(rb_cRange, "min", range_min, -1);
rb_define_method(rb_cRange, "max", range_max, -1);
+ rb_define_method(rb_cRange, "minmax", range_minmax, 0);
rb_define_method(rb_cRange, "size", range_size, 0);
rb_define_method(rb_cRange, "to_a", range_to_a, 0);
rb_define_method(rb_cRange, "entries", range_to_a, 0);