1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
knu 2007-12-10 13:25:39 +00:00
parent 6d7999c132
commit d2e596ad04
3 changed files with 33 additions and 2 deletions

View file

@ -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.

14
array.c
View file

@ -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));

View file

@ -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