String subclass method returns subclass - bug or feature?

Discussion in 'Ruby' started by S.Volkov, Mar 11, 2006.

  1. S.Volkov

    S.Volkov Guest

    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
    S.Volkov, Mar 11, 2006
    #1
    1. Advertising

  2. S.Volkov

    George Ogata Guest

    "S.Volkov" <> 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 <>

    * 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.
    George Ogata, Mar 12, 2006
    #2
    1. Advertising

  3. S.Volkov

    S.Volkov Guest

    Re: String subclass method returns subclass - intentional feature

    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" <> wrote in message
    news:...
    > "S.Volkov" <> 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 <>
    >
    > * 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.
    S.Volkov, Mar 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    431
    jstorta
    Feb 20, 2006
  2. Simon Strandgaard

    [bug] String#split returns extra empty string

    Simon Strandgaard, May 31, 2004, in forum: Ruby
    Replies:
    8
    Views:
    324
    David A. Black
    Jun 1, 2004
  3. Tammo Freese
    Replies:
    16
    Views:
    174
  4. Trans
    Replies:
    8
    Views:
    311
    Robert Klemme
    Oct 23, 2008
  5. robic0
    Replies:
    7
    Views:
    101
    Samwyse
    Jan 24, 2006
Loading...

Share This Page