diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-08-06 12:59:59 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-08-06 12:59:59 +0000 |
commit | 9deecfc4531cc038a0c27a72c316dafb9aa2bc23 (patch) | |
tree | 41cc1280bbd257487739a78032c874bd6583b0a3 | |
parent | 814b7b544848ffd41e97b3a0af2db7baee146764 (diff) |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | range.c | 11 | ||||
-rw-r--r-- | test/ruby/test_range.rb | 2 |
3 files changed, 16 insertions, 2 deletions
@@ -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): @@ -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 |