ruby-changes:30345
From: nobu <ko1@a...>
Date: Tue, 6 Aug 2013 17:43:03 +0900 (JST)
Subject: [ruby-changes:30345] nobu:r42400 (trunk): range.c: consider exclusive
nobu 2013-08-06 17:42:51 +0900 (Tue, 06 Aug 2013) New Revision: 42400 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42400 Log: range.c: consider exclusive * range.c (range_last): exclude the last number of the exclusive range if the end is Numeric. [ruby-dev:47587] [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 42399) +++ ChangeLog (revision 42400) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Aug 6 17:42:47 2013 Nobuyoshi Nakada <nobu@r...> + + * range.c (range_last): exclude the last number of the exclusive range + if the end is Numeric. [ruby-dev:47587] [Bug #8739] + Tue Aug 6 17:42:21 2013 Nobuyoshi Nakada <nobu@r...> * win32/win32.c (rb_w32_conv_from_wchar): converted string to CP_UTF8 Index: range.c =================================================================== --- range.c (revision 42399) +++ range.c (revision 42400) @@ -887,7 +887,15 @@ range_first(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/range.c#L887 static VALUE range_last(int argc, VALUE *argv, VALUE range) { - if (argc == 0) return RANGE_END(range); + if (argc == 0) { + 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); + + /* fallback to Array */ + } return rb_ary_last(argc, argv, rb_Array(range)); } Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 42399) +++ test/ruby/test_range.rb (revision 42400) @@ -250,6 +250,18 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L250 def test_first_last assert_equal([0, 1, 2], (0..10).first(3)) assert_equal([8, 9, 10], (0..10).last(3)) + assert_equal(0, (0..10).first) + assert_equal(10, (0..10).last) + assert_equal("a", ("a".."c").first) + assert_equal("c", ("a".."c").last) + + bug8739 = '[ruby-dev:47587] [Bug #8739] from exclusive range' + assert_equal([0, 1, 2], (0...10).first(3), bug8739) + assert_equal([7, 8, 9], (0...10).last(3), bug8739) + assert_equal(0, (0...10).first, bug8739) + assert_equal(9, (0...10).last, bug8739) + assert_equal("a", ("a"..."c").first, bug8739) + assert_equal("b", ("a"..."c").last, bug8739) end def test_to_s -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/