From: Bob Alexander Date: 2002-01-26T14:20:32+09:00 Subject: Re: Subrange of String subclass => invalid object wrote: > What did you expect? > ----------------------------------------- > S(otherValue: 77, "abcdefghij") > ./rbug.rb:15:in `initialize: wrong number of arguments(1 for 2) (ArgumentError) > from ./rbug.rb:15:in `[]' > from ./rbug.rb:15 > ----------------------------------------- > Or? > ----------------------------------------- > S(otherValue: 77, "abcdefghij") > S(otherValue: 77, "bcdefghi") > ----------------------------------------- Well, my example was pretty simple, but in a real life case - It's a pretty good bet that leaving the instance variable uninitialized does not result in a valid object. - 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. This is a difficult problem. The current solution is too sloppy for such a well thought out language as Ruby. I hope collectively the community can come up with a better way. 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. Bob