summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--range.c11
-rw-r--r--test/ruby/test_range.rb2
3 files changed, 16 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 04071e9667..0de218dd4e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Aug 6 21:59:56 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * range.c (range_last): return nil for empty range, or in the case the
+ predecessor is smaller than the begin. [Bug #8739]
+
Tue Aug 6 21:48:31 2013 Kouji Takao <kouji.takao@gmail.com>
* ext/readline/readline.c (readline_s_set_point, Init_readline):
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 */
}
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index c50139ef9c..d738e56294 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -254,6 +254,7 @@ class TestRange < Test::Unit::TestCase
assert_equal(10, (0..10).last)
assert_equal("a", ("a".."c").first)
assert_equal("c", ("a".."c").last)
+ assert_equal(0, (2..0).last)
bug8739 = '[ruby-dev:47587] [Bug #8739] from exclusive range'
assert_equal([0, 1, 2], (0...10).first(3), bug8739)
@@ -262,6 +263,7 @@ class TestRange < Test::Unit::TestCase
assert_equal(9, (0...10).last, bug8739)
assert_equal("a", ("a"..."c").first, bug8739)
assert_equal("b", ("a"..."c").last, bug8739)
+ assert_nil((2...0).last)
end
def test_to_s