Project

General

Profile

Bug #10845

Subclassing String

Added by sawa (Tsuyoshi Sawada) over 5 years ago. Updated 8 months ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:68084]

Description

If I make a subclass of String, the method * returns an instance of that class.

class MyString < String
end

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

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

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?


Related issues

Related to Ruby master - Bug #6087: How should inherited methods deal with return values of their own subclass? Assignedmatz (Yukihiro Matsumoto)Actions
Has duplicate Ruby master - Bug #11209: [PATCH] Fix for String#+ when subclassedClosedActions

Also available in: Atom PDF