"S.Volkov" <svolkov / comcast.net> 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 )
* [ruby-talk:55268] ( http://rubyurl.com/T5G )
* [ruby-core:801]   ( 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 Matsumoto  <matz / ruby-lang.org>

        * 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.