diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-10 13:25:39 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-10 13:25:39 +0000 |
commit | d2e596ad04050ebfe575106ea815849aefb689b2 (patch) | |
tree | b0263011852abcd32f8d941951ffa11c02e6e592 | |
parent | 6d7999c13285a7a2bec9073504703226e1a2b2f2 (diff) | |
download | ruby-d2e596ad04050ebfe575106ea815849aefb689b2.tar.gz |
* array.c (rb_ary_slice_bang): If an invalid range is given, do
not raise an exception but return nil just like slice() does.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14183 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | array.c | 14 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 16 |
3 files changed, 33 insertions, 2 deletions
@@ -1,3 +1,8 @@ +Mon Dec 10 22:08:47 2007 Akinori MUSHA <knu@iDaemons.org> + + * array.c (rb_ary_slice_bang): If an invalid range is given, do + not raise an exception but return nil just like slice() does. + Mon Dec 10 21:47:53 2007 Nobuyoshi Nakada <nobu@ruby-lang.org> * transcode.c (str_transcode): allow non-registered encodings. @@ -1895,8 +1895,18 @@ rb_ary_slice_bang(int argc, VALUE *argv, VALUE ary) return arg2; } - if (!FIXNUM_P(arg1) && rb_range_beg_len(arg1, &pos, &len, RARRAY_LEN(ary), 1)) { - goto delete_pos_len; + if (!FIXNUM_P(arg1)) { + switch (rb_range_beg_len(arg1, &pos, &len, RARRAY_LEN(ary), 0)) { + case Qtrue: + /* valid range */ + goto delete_pos_len; + case Qnil: + /* invalid range */ + return Qnil; + default: + /* not a range */ + break; + } } return rb_ary_delete_at(ary, NUM2LONG(arg1)); diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 6787d9c99c..c0206baed5 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1031,6 +1031,8 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[10, 11, 12], a.slice(9, 3)) assert_equal(@cls[10, 11, 12], a.slice(-91, 3)) + assert_nil(a.slice(-101, 2)) + assert_equal(@cls[1], a.slice(0..0)) assert_equal(@cls[100], a.slice(99..99)) assert_equal(@cls[], a.slice(100..100)) @@ -1041,6 +1043,8 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[10, 11, 12], a.slice(9..11)) assert_equal(@cls[10, 11, 12], a.slice(-91..-89)) + assert_nil(a.slice(-101..-1)) + assert_nil(a.slice(10, -3)) # Ruby 1.8 feature change: # Array#slice[size..x] always returns []. @@ -1072,6 +1076,18 @@ class TestArray < Test::Unit::TestCase a = @cls[1, 2, 3, 4, 5] assert_equal(nil, a.slice!(20)) assert_equal(@cls[1, 2, 3, 4, 5], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(nil, a.slice!(-6)) + assert_equal(@cls[1, 2, 3, 4, 5], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(nil, a.slice!(-6..4)) + assert_equal(@cls[1, 2, 3, 4, 5], a) + + a = @cls[1, 2, 3, 4, 5] + assert_equal(nil, a.slice!(-6,2)) + assert_equal(@cls[1, 2, 3, 4, 5], a) end def test_sort |