# String subclass method returns subclass - bug or feature?

**URL:** <https://www.ruby-forum.com/t/string-subclass-method-returns-subclass-bug-or-feature/79823>\
**Category:** Ruby\
**Created:** [March 11, 2006, 7:01am UTC](https://www.ruby-forum.com/t/string-subclass-method-returns-subclass-bug-or-feature/79823 "2006-03-11T07:01:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S.Volkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@S.Volkov](https://www.ruby-forum.com/u/S.Volkov)\
**Post date:** [March 11, 2006, 7:01am UTC](https://www.ruby-forum.com/t/string-subclass-method-returns-subclass-bug-or-feature/79823/1 "2006-03-11T07:01:19Z")

</div>

Could you please explain me why String instance methods return subclass,  
and MyStr#to\_s returns String, not the receiver as specified?

# here is the test code

$ cat tStringSubClass.rb  
class MyStr \< String  
def rest  
self[1…-1]  
end  
end  
ms = MyStr.new(‘ABC’)  
puts   
'I got ’ + ms.class.name,  
‘Expect String, get:’,  
ms.rest.class,  
ms.downcase.class,  
ms.succ.class,  
ms[1…-1].class,  
ms.split(//).map{ |s| s.class },  
‘Expect MyStr, get:’,  
ms.to\_s.class,  
ms.to\_str.class

# and here is the result

## $ ruby tStringSubClass.rb I got MyStr Expect String, get: MyStr MyStr MyStr MyStr MyStr MyStr MyStr Expect MyStr, get: String String

thanks  
brs  
Sergey

---

<div class="post-metadata">

**Author:** ![S.Volkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@S.Volkov](https://www.ruby-forum.com/u/S.Volkov)\
**Post date:** [March 12, 2006, 8:06am UTC](https://www.ruby-forum.com/t/string-subclass-method-returns-subclass-bug-or-feature/79823/2 "2006-03-12T08:06:23Z")

</div>

“S.Volkov” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Could you please explain me why String instance methods return subclass,  
> and MyStr#to\_s returns String, not the receiver as specified?

String (and Array) methods that return new strings (arrays) tend to  
return instances of the receiver’s class rather than String. This  
often makes subclassing a bit less painful, although a couple of pies  
have been flung over it too:

- [ruby-talk:54225] ( [http://rubyurl.com/dv2](http://rubyurl.com/dv2) )
- [ruby-talk:55268] ( [http://rubyurl.com/T5G](http://rubyurl.com/T5G) )
- [ruby-core:801] ( [http://rubyurl.com/Ic6](http://rubyurl.com/Ic6) )

That last thread is probably what led to the following Changelog  
entry, which explains the behaviour of MyStr#to\_s you saw.

Mon Feb 10 10:14:26 2003 Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

```
    * array.c (rb_ary_to_a): return value should be an Array if the
  receiver is an instance of subclass of Array.

    * string.c (rb_str_to_s): return value should be a String if the
  receiver is an instance of subclass of String.

```

---

<div class="post-metadata">

**Author:** ![S.Volkov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@S.Volkov](https://www.ruby-forum.com/u/S.Volkov)\
**Post date:** [March 12, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/string-subclass-method-returns-subclass-bug-or-feature/79823/3 "2006-03-12T19:51:17Z")

</div>

Thanks for references,  
I understand that it was discussed already, and decision was made;  
imho: it’s irregular and not OO, but who cares about formal, theoretical  
correctness, ‘be practical’ criteria rules!

Regarding ‘to\_s, to\_str returning String’: hope documentation will be  
fixed;

regards  
Sergey

“George O.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…
