[前][次][番号順一覧][スレッド一覧]

ruby-changes:30349

From: nobu <ko1@a...>
Date: Tue, 6 Aug 2013 22:00:10 +0900 (JST)
Subject: [ruby-changes:30349] nobu:r42407 (trunk): range.c: return nil for empty range

nobu	2013-08-06 21:59:59 +0900 (Tue, 06 Aug 2013)

  New Revision: 42407

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42407

  Log:
    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]

  Modified files:
    trunk/ChangeLog
    trunk/range.c
    trunk/test/ruby/test_range.rb
_______________________________________________
ruby-cvs mailing list
ruby-cvs@r...
http://lists.ruby-lang.org/cgi-bin/mailman/listinfo/ruby-cvs
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42406)
+++ ChangeLog	(revision 42407)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Aug  6 21:59:56 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@g...>
 
 	* ext/readline/readline.c (readline_s_set_point, Init_readline):
Index: range.c
===================================================================
--- range.c	(revision 42406)
+++ range.c	(revision 42407)
@@ -891,8 +891,15 @@ range_last(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L891
 	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 */
     }
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 42406)
+++ test/ruby/test_range.rb	(revision 42407)
@@ -254,6 +254,7 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L254
     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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L263
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]