From 9deecfc4531cc038a0c27a72c316dafb9aa2bc23 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 6 Aug 2013 12:59:59 +0000 Subject: range.c: return nil for empty range * range.c (range_last): return nil for empty range, or in the case the predecessor is smaller than the begin. [Bug #8739] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42407 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- range.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'range.c') diff --git a/range.c b/range.c index c805848abd..007625469e 100644 --- a/range.c +++ b/range.c @@ -891,8 +891,15 @@ range_last(int argc, VALUE *argv, VALUE range) VALUE e = RANGE_END(range); if (!EXCL(range)) return e; /* inclusive, the end is the last */ /* exclusive, the last is previous to the end */ - if (FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric)) - return rb_int_pred(e); + if (FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric)) { + VALUE pred = rb_int_pred(e); + if (!r_lt(RANGE_BEG(range), pred)) { + /* TODO: what should be returned, or should raise an + * exception? */ + pred = Qnil; + } + return pred; + } /* fallback to Array */ } -- cgit v1.2.3