Issue #10845 has been updated by jeremyevans0 (Jeremy Evans).


I've added a pull request for making String methods consistently return String instances when called on String subclasses: https://github.com/ruby/ruby/pull/3701

For the reasons @marcandre mentioned, I think this makes more sense.  Instance specific state is not copied over, and you can end up with invalid objects.  If we are going to make similar changes for Array in 3.0 (see #6087), I think it's worth considering the same change for String.

----------------------------------------
Bug #10845: Subclassing String
https://bugs.ruby-lang.org/issues/10845#change-88168

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* ruby -v: 2.2
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
If I make a subclass of `String`, the method `*` returns an instance of that class.

~~~ruby
class MyString < String
end

MyString.new("foo").*(2).class #=> MyString
~~~

This is different from other similar operations like `+` and `%`, which return a `String` instance.

~~~ruby
MyString.new("foo").+("bar").class #=> String
MyString.new("%{foo}").%(foo: "bar").class #=> String
~~~

I don't see clear reason why `*` is to be different from `+` and `%`, and thought that perhaps either the behaviour with `*` is a bug, or the behaviour with `+` and `%` is a bug.

Or, is a reason why they are different?



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>