"Bob Alexander" <bobalex / attbi.com> wrote in,
....
> Thanks for the pointers. I read those, and saw no mention of the fact that
> String and Array returning uninitialized instances of a subclass creates
> invalid objects. The focus seemed to be on trivial subclasses that simply
> added methods but no state, where no subclass initialization needs to be
> done.

Well it seems that our POLS are different as well - I would not expect
that subclassing modifies the return value of a method unless it is a
self-modifing like String#capitalize! or Array#sort! or one of the
fundamental copying methods Object#copy and Object#dup.

A good example is
-----
class List < Array; end
i = 0
L = List.new(5).collect!{ i+=1 }
p    L.type                                         # => List
p    L.select   {|i|  i % 2 == 0 }.type   # => Array
p    L.collect  {|i|  i*i }.type              # => Array
p    L.collect! {|i|  i*i }.type              # => List
----

Anyway in your particular example it does not seem apparent
if  sub_indexing  makes a copy of your instance variable or not -
i.e. it seems to me that your responsibility to say

----
class S < String
    # your example
    def [](r)
        (res = super(r)).otherValue = @otherValue        # or
        #  (res = super(r)).otherValue = @otherValue.dup #
        return res
     end
    protected
    attr_writer :otherValue
end
----

Note with some work you can also automate this process by
overriding String#inherited

/Christoph


/Christoph