diff options
author | Jeremy Evans <code@jeremyevans.net> | 2020-10-24 11:52:30 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2020-11-20 16:30:23 -0800 |
commit | 58325daae3beefda13ed100782cd19a89cc68771 (patch) | |
tree | fd1857904c1c86d9c7c21f6038535f3beeafc4bb /spec/ruby/core/string/shared/each_line.rb | |
parent | 4f5d14eb8cb7fba1c583d516d597c5e28d7a5540 (diff) |
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]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3701
Diffstat (limited to 'spec/ruby/core/string/shared/each_line.rb')
-rw-r--r-- | spec/ruby/core/string/shared/each_line.rb | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/spec/ruby/core/string/shared/each_line.rb b/spec/ruby/core/string/shared/each_line.rb index 156565ff13..f9c910596a 100644 --- a/spec/ruby/core/string/shared/each_line.rb +++ b/spec/ruby/core/string/shared/each_line.rb @@ -93,10 +93,20 @@ describe :string_each_line, shared: true do end end - it "yields subclass instances for subclasses" do - a = [] - StringSpecs::MyString.new("hello\nworld").send(@method) { |s| a << s.class } - a.should == [StringSpecs::MyString, StringSpecs::MyString] + ruby_version_is ''...'3.0' do + it "yields subclass instances for subclasses" do + a = [] + StringSpecs::MyString.new("hello\nworld").send(@method) { |s| a << s.class } + a.should == [StringSpecs::MyString, StringSpecs::MyString] + end + end + + ruby_version_is '3.0' do + it "yields String instances for subclasses" do + a = [] + StringSpecs::MyString.new("hello\nworld").send(@method) { |s| a << s.class } + a.should == [String, String] + end end it "returns self" do |