Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String subclass method returns subclass - bug or feature?

Reply
Thread Tools

String subclass method returns subclass - bug or feature?

 
 
S.Volkov
Guest
Posts: n/a
 
      03-11-2006
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


 
Reply With Quote
 
 
 
 
George Ogata
Guest
Posts: n/a
 
      03-12-2006
"S.Volkov" <(E-Mail Removed)> 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 <(E-Mail Removed)>

* 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.
 
Reply With Quote
 
 
 
 
S.Volkov
Guest
Posts: n/a
 
      03-12-2006
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 Ogata" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "S.Volkov" <(E-Mail Removed)> 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 <(E-Mail Removed)>
>
> * 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclass of subclass Fab C++ 0 08-09-2012 09:54 AM
subclass a class in the namespace of the that subclass Trans Ruby 8 10-23-2008 07:24 AM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM
[bug] String#split returns extra empty string Simon Strandgaard Ruby 8 06-01-2004 01:52 PM



Advertisments