mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Make String methods return String instances when called on a subclass instance
This modifies the following String methods to return String instances instead of subclass instances: * String#* * String#capitalize * String#center * String#chomp * String#chop * String#delete * String#delete_prefix * String#delete_suffix * String#downcase * String#dump * String#each/#each_line * String#gsub * String#ljust * String#lstrip * String#partition * String#reverse * String#rjust * String#rpartition * String#rstrip * String#scrub * String#slice! * String#slice/#[] * String#split * String#squeeze * String#strip * String#sub * String#succ/#next * String#swapcase * String#tr * String#tr_s * String#upcase This also fixes a bug in String#swapcase where it would return the receiver instead of a copy of the receiver if the receiver was the empty string. Some string methods were left to return subclass instances: * String#+@ * String#-@ Both of these methods will return the receiver (subclass instance) in some cases, so it is best to keep the returned class consistent. Fixes [#10845]
This commit is contained in:
parent
4f5d14eb8c
commit
58325daae3
Notes:
git
2020-11-21 09:30:48 +09:00
28 changed files with 591 additions and 211 deletions
|
@ -142,12 +142,21 @@ describe "String#slice! with index, length" do
|
|||
"hello".slice!(obj, obj).should == "ll"
|
||||
end
|
||||
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(0, 0).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(0, 4).should be_an_instance_of(StringSpecs::MyString)
|
||||
ruby_version_is ''...'3.0' do
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(0, 0).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(0, 4).should be_an_instance_of(StringSpecs::MyString)
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is '3.0' do
|
||||
it "returns String instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(0, 0).should be_an_instance_of(String)
|
||||
s.slice!(0, 4).should be_an_instance_of(String)
|
||||
end
|
||||
end
|
||||
|
||||
it "returns the substring given by the character offsets" do
|
||||
"hellö there".slice!(1,0).should == ""
|
||||
|
@ -196,10 +205,20 @@ describe "String#slice! Range" do
|
|||
end
|
||||
end
|
||||
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(0...0).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(0..4).should be_an_instance_of(StringSpecs::MyString)
|
||||
ruby_version_is ''...'3.0' do
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(0...0).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(0..4).should be_an_instance_of(StringSpecs::MyString)
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is '3.0' do
|
||||
it "returns String instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(0...0).should be_an_instance_of(String)
|
||||
s.slice!(0..4).should be_an_instance_of(String)
|
||||
end
|
||||
end
|
||||
|
||||
it "calls to_int on range arguments" do
|
||||
|
@ -299,10 +318,20 @@ describe "String#slice! with Regexp" do
|
|||
end
|
||||
end
|
||||
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(//).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(/../).should be_an_instance_of(StringSpecs::MyString)
|
||||
ruby_version_is ''...'3.0' do
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(//).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(/../).should be_an_instance_of(StringSpecs::MyString)
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is '3.0' do
|
||||
it "returns String instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(//).should be_an_instance_of(String)
|
||||
s.slice!(/../).should be_an_instance_of(String)
|
||||
end
|
||||
end
|
||||
|
||||
it "returns the matching portion of self with a multi byte character" do
|
||||
|
@ -383,10 +412,20 @@ describe "String#slice! with Regexp, index" do
|
|||
"har".slice!(/(.)(.)(.)/, obj).should == "a"
|
||||
end
|
||||
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(/(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(/(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString)
|
||||
ruby_version_is ''...'3.0' do
|
||||
it "returns subclass instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(/(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString)
|
||||
s.slice!(/(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString)
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is '3.0' do
|
||||
it "returns String instances" do
|
||||
s = StringSpecs::MyString.new("hello")
|
||||
s.slice!(/(.)(.)/, 0).should be_an_instance_of(String)
|
||||
s.slice!(/(.)(.)/, 1).should be_an_instance_of(String)
|
||||
end
|
||||
end
|
||||
|
||||
it "returns the encoding aware capture for the given index" do
|
||||
|
@ -461,11 +500,22 @@ describe "String#slice! with String" do
|
|||
-> { "hello".slice!(o) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "returns a subclass instance when given a subclass instance" do
|
||||
s = StringSpecs::MyString.new("el")
|
||||
r = "hello".slice!(s)
|
||||
r.should == "el"
|
||||
r.should be_an_instance_of(StringSpecs::MyString)
|
||||
ruby_version_is ''...'3.0' do
|
||||
it "returns a subclass instance when given a subclass instance" do
|
||||
s = StringSpecs::MyString.new("el")
|
||||
r = "hello".slice!(s)
|
||||
r.should == "el"
|
||||
r.should be_an_instance_of(StringSpecs::MyString)
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is '3.0' do
|
||||
it "returns a subclass instance when given a subclass instance" do
|
||||
s = StringSpecs::MyString.new("el")
|
||||
r = "hello".slice!(s)
|
||||
r.should == "el"
|
||||
r.should be_an_instance_of(String)
|
||||
end
|
||||
end
|
||||
|
||||
it "raises a FrozenError if self is frozen" do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue