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

Let String#slice! return nil (#3533)

Returns `nil` instead of an empty string when non-integer number is given (to make it 2.7 compatible).
This commit is contained in:
Soutaro Matsumoto 2020-09-11 14:34:10 +09:00 committed by GitHub
parent 0d78390bfb
commit f0ddbd502c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2020-09-11 14:34:35 +09:00
Merged-By: soutaro <matsumoto@soutaro.com>
2 changed files with 6 additions and 1 deletions

View file

@ -4961,7 +4961,10 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
return Qnil;
case Qfalse:
beg = NUM2LONG(indx);
goto num_index;
if (!(p = rb_str_subpos(str, beg, &len))) return Qnil;
if (!len) return Qnil;
beg = p - RSTRING_PTR(str);
goto subseq;
default:
goto num_index;
}

View file

@ -1588,8 +1588,10 @@ CODE
a = S("FooBar")
if @aref_slicebang_silent
assert_nil( a.slice!(6) )
assert_nil( a.slice!(6r) )
else
assert_raise(IndexError) { a.slice!(6) }
assert_raise(IndexError) { a.slice!(6r) }
end
assert_equal(S("FooBar"), a)