summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-06 08:42:51 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-06 08:42:51 +0000
commit2743b0d6082561a55c6a0d0843201f2413de93f3 (patch)
tree286e96129e18c2067016fcf0e7e645fb4785645f
parentb0391268a221f233f098e39e3bc21b8a0888509e (diff)
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--range.c10
-rw-r--r--test/ruby/test_range.rb12
3 files changed, 26 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index e643a13374..89e4118351 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Aug 6 17:42:47 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* win32/win32.c (rb_w32_conv_from_wchar): converted string to CP_UTF8
diff --git a/range.c b/range.c
index 8fa7dcb3ba..c805848abd 100644
--- a/range.c
+++ b/range.c
@@ -887,7 +887,15 @@ range_first(int argc, VALUE *argv, VALUE range)
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));
}
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index e12e8fc438..c50139ef9c 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -250,6 +250,18 @@ class TestRange < Test::Unit::TestCase
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