From: Sean O'Dell Date: 2004-06-29T08:50:13+09:00 Subject: Re: Is it considered Harmful? On Monday 28 June 2004 16:01, Gavin Sinclair wrote: > On Tuesday, June 29, 2004, 4:04:21 AM, Sean wrote: > >> You're missing his point. > >> > >> If I call Array.new, I certainly get an array whose #initialize > >> has been called. > >> > >> But if I then do arr.class = Net::HTTP, then Net:HTTP#initialize > >> does NOT get called (e.g., to set the host name or whatever). > > > > Ah...I see why I haven't connected with that issue now. I think of this > > as a "buyer beware" issue. If you change an existing object's class, be > > sure you change it to something that understands the object it is > > receiving. I think even #become would have these problems. > > No it wouldn't. Objects are supposed to represent data and behaviour. > #become keeps both intact. #class= changes only the behaviour. If > something does't work after #become, it's not because the use of > #become introduced an inconsistency. The same cannot be said of > #class=. That's not necessarily a point against it... I realized my error regarding #become after reading another post. > > Even using include could introduce methods to a class which didn't > > initialize instance data the way it expects. > > ...as this demonstrates. Ruby contains lots of features that make it > easy to write broken programs. #class= should be considered in that > light. I don't see it as being very useful (stateless behaviour can > be elegantly captured by modules), but I'm sure it could be. I think both #class= and #become have a lot of the same potential pitfalls, but I agree that #become encourages better data encapsulation. Assigning the data of an object to another class arbitrarily (such as is possible with #class=), in OOP terms, is a bit out there. I should try and remember how well Ruby implements OOP and how such suggestions would really rub people the wrong way who want to preserve that. I was only concerned with keeping Ruby from crashing, and adding C code to protect it more, and I really didn't consider how it would affect Ruby's OO-ness (I didn't say paradigm). Sean O'Dell