From: nobu.nokada@... Date: 2002-01-27T13:56:53+09:00 Subject: Re: Subrange of String subclass => invalid object Hi, At Sat, 26 Jan 2002 14:20:32 +0900, Bob Alexander wrote: > - It's a pretty good bet that leaving the instance variable uninitialized > does not result in a valid object. Agree. > - We have no way of knowing whether copying the value (77) from the > original instance results in a valid object. Maybe it's a hash of the > string, or an object sequence number, in which case it should be something > different from 77. > > I don't know of a foolproof way for a superclass to create a valid instance > of the subclass without knowledge of the subclass. How about this? class S < String def [](*) dup.replace(super) end end It works as this right now. class S < String def [](*) type.allocate.replace(super) end end > A simple answer that probably most folks won't like is: the superclass could > return an instance of itself (seems natural enough!). The subclass can then > do what it wants with it (in an overriding method), including returning a > new subclass instance, modifying the existing self instance, returning the > superclass instance, or anything else it chooses. This is a bit more work > for the subclasser, but it is airtight. Isn't it latter of [ruby-talk:32462]? -- Nobu Nakada