Project

General

Profile

Actions

Bug #17014

closed

Range#minmax returns incorrect results on non-numeric exclusive ranges

Added by sambostock (Sam Bostock) over 4 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:99071]

Description

The implementation of Range#minmax added in d5c60214c45 causes the following incorrect behaviour:

('a'...'c').minmax # => ["a", ["a", "b"]]

instead of

('a'...'c').minmax # => ["a", "b"]

Cause

This is because the C implementation of Range#minmax (range_minmax) directly delegates to the C implementation of Range#min (range_min) and Range#max (range_max), without changing the execution context.

Range#max's C implementation (range_max), when given a non-numeric exclusive range, delegates to super, which is meant to call Enumerable#max. However, because range_max is called directly by range_minmax, super calls Enumerable#minmax instead, causing the incorrect nesting.

Resolution

  • ruby/ruby#3285 fixed this bug by explicitly calling Range#min and Range#max, instead of delegating directly to range_min and range_max
  • ruby/ruby#3286 followed up by replacing rb_intern("min") and rb_intern("max") in the new implementation with statics id_min and id_max
  • ruby/ruby#3290 follows up by extracting range_min_internal and range_max_internal from range_min and range_max, and calling those directly from range_minmax
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0