From: Rich Kilmer Date: 2002-04-01T22:17:41+09:00 Subject: RE: Creating an instance of a class without calling initialize() > -----Original Message----- > From: Harry Ohlsen [mailto:harryo@zip.com.au] > Sent: Saturday, March 30, 2002 6:08 PM > To: ruby-talk ML > Subject: Re: Creating an instance of a class without calling initialize() > > "Joseph Girgis" wrote in message > news:... > > Why don't you want the initialize() method? > > Glad someone finally asked :-). > > I've been working with Chris Morris's XML serialisation module, which is > really nice. However, what it currently requires is that your initialize > either takes no parameters or has defaults for everything, so that it can > be called with none. What the deserialisation does is something like ... > > obj = new > > FOR EACH ATTRIBUTE > obj.send("#{attributeName}=", attributeValue) > END You actually may want to recommend that he use: obj = new FOR EACH ATTRIBUTE obj.instance_eval "@#{attributeName}=#{attributeValue}" END That way it works for attributes that do not have writer methods but only reader methods (ie. Foo.x but not Foo.x=) > > where the attribute names and values are retrieved from the XML data. > > However, we've been discussing how this is a bit of a restriction, > especially if you're retrofitting the serialisation to a class > hierarchy you've already written. > > What you really want is to bypass any initialize that exists, not > force the class designer to make their initialize callable with > no parameters. > > I'd been able to make everything work the way I want, but only by > having a separate method in every class that generates a prototype > object (ie, it just calls initialize with some valid set of values) > and wanted to remove that requirement. > > The idea about aliasing and then unaliasing initialize should work > for me; I really should have thought of that !! I'm also intrigued > by the reference to YourClass.allocate() mechanism that's been > mentioned as being in 1.7, so I'll look into that. > > Thanks to everyone for the replies. I just KNEW it could be done in > pure Ruby, but wasn't thinking carefully enough !!