diff options
author | Sam Bostock <sam.bostock@shopify.com> | 2020-07-03 22:56:07 -0400 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2020-07-04 10:02:24 -0700 |
commit | bf1a6771f305ea286a3ae575676924551c03e857 (patch) | |
tree | aa12dbd8c25e1072ab5aa9714dee5431b17d8ded /range.c | |
parent | 9fc564cfef22a2a4eb7af8b2e1df84e4d473cae6 (diff) |
Fix non-numeric exclusive Range#minmax bug
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"]
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.
Perhaps it is possible to change the execution context in an optimized
manner, but the simplest solution seems to be to just explicitly
delegate from Range#minmax to Range#min and Range#max.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3285
Diffstat (limited to 'range.c')
-rw-r--r-- | range.c | 5 |
1 files changed, 4 insertions, 1 deletions
@@ -1266,7 +1266,10 @@ 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)); + return rb_assoc_new( + rb_funcall(range, rb_intern("min"), 0), + rb_funcall(range, rb_intern("max"), 0) + ); } int |