summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2020-07-16 10:11:35 -0700
committerMarc-Andre Lafortune <github@marc-andre.ca>2020-07-19 10:25:55 -0400
commit05bf811c2839628aaef3d565daedb28be80d47ef (patch)
tree7d698b91dbd823306db5efc752e9cae3e4f6b569 /range.c
parentd637208abd0ae7ccf0539679ca52df1caada4db7 (diff)
Special case Range#max for integer beginning and Float::Infinity end
Popular Ruby libraries such as Rails and Rubocop relying on the previous behavior, even though it is technically a bug. The correct behavior is probably raising RangeError, since that is what an endless range raises. Related to [Bug #17017]
Diffstat (limited to 'range.c')
-rw-r--r--range.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/range.c b/range.c
index 124d9faa51..6d507ea124 100644
--- a/range.c
+++ b/range.c
@@ -1255,6 +1255,15 @@ range_max(int argc, VALUE *argv, VALUE range)
return rb_funcall(e, '-', 1, INT2FIX(1));
}
if (RB_INTEGER_TYPE_P(b) && !RB_INTEGER_TYPE_P(e)) {
+ if (RB_TYPE_P(e, T_FLOAT)) {
+ VALUE inf = rb_funcall(e, rb_intern("infinite?"), 0);
+ if (inf != Qnil) {
+ /* For backwards compatibility, return end if the end
+ * is Float::Infinity and the beginning is integer.
+ If end is -Float::Infinity, return nil. */
+ return(inf == INT2FIX(1) ? e : Qnil);
+ }
+ }
e = rb_funcall(e, rb_intern("floor"), 0);
}
return e;