ruby-changes:62888
From: Soutaro <ko1@a...>
Date: Fri, 11 Sep 2020 14:34:27 +0900 (JST)
Subject: [ruby-changes:62888] f0ddbd502c (master): Let String#slice! return nil (#3533)
https://git.ruby-lang.org/ruby.git/commit/?id=f0ddbd502c From f0ddbd502c1d6912cec9a91997966ba659e347c1 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto <matsumoto@s...> Date: Fri, 11 Sep 2020 14:34:10 +0900 Subject: 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). diff --git a/string.c b/string.c index 7cfeaae..0c4e76a 100644 --- a/string.c +++ b/string.c @@ -4961,7 +4961,10 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L4961 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; } diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index b2f0289..fde1c9c 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1588,8 +1588,10 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1588 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) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/