From: Nathan Taylor-Hoover Date: 2007-06-14T00:23:39+09:00 Subject: Re: Code refactoring advice ------=_Part_23897_17169718.1181748219871 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Thanks again! I have the pickaxe2, and the "ruby way" book however I couldn't figure out from their documentation if you could include calls to other classes without extending them (probably due to information overload with all the advanced OO documentation). Also they seem to touch a little lightly on the subclassing and clearer details on how to handle the initialize method. These examples answered all my questions. Glad I joined the mailing list! :D - Nathan On 6/11/07, Jano Svitok wrote: > > On 6/11/07, Nathan Taylor-Hoover wrote: > > Thank you very much Rick. That was exactly the kind of advice I was > looking > > for. I really didn't need the OO capabilities. Was just looking for a > way > > to define functions basically. Ended up writing the code as below since > I > > am expanding it until it all works fine, and then including it into a > larger > > app. > > You can wrap this in a class (depending on what do you need to do with > the request. > If you don't do anything more than you have written, do (change the > name of the class as appropriate) > > > > #!/usr/bin/ruby > > > > require 'rubygems' > > require 'xml_struct' > > require 'uuidtools' > > class XmlRequest > XMLVER = "\n" > > def initialize > > @request = XmlStruct.new > > msguuid = UUID.random_create.to_s > > rqstuuid = UUID.random_create.to_s > > @request.BigWrapR = {:xmlns => "BurgerSpace"} > > @request.BigWrapR.MyVer="1.0" > ... > > @request.BigWrapR.Verg.Stuff.MoreStuff.AnTag > > @request.BigWrapR.Verg.MsgData > > end > > > > def namespace=(namestring) > > @request.BigWrapR = {:xmlns => "#{namestring}"} > > end > > def to_s > XMLVER + @request.to_s > end > end > > request = XmlRequest.new > request.namespace = "newNameSpace" > puts request.to_s > > Another possibility is to derive from XmlStruct (I don't know it, so > it's possible this will not > work): > > class XmlRequest < XmlStruct > XMLVER = "\n" > > def initialize > super > > msguuid = UUID.random_create.to_s > > rqstuuid = UUID.random_create.to_s > > self.BigWrapR = {:xmlns => "BurgerSpace"} > > self.BigWrapR.MyVer="1.0" > ... > > self.BigWrapR.Verg.Stuff.MoreStuff.AnTag > > self.BigWrapR.Verg.MsgData > > end > > > > def namespace=(namestring) > > self.BigWrapR = {:xmlns => "#{namestring}"} > > end > > def to_s > XMLVER + super > end > end > > request = XmlRequest.new > request.namespace = "newNameSpace" > puts request.to_s > > The advantage of having all this wrapped in a class is two fold: > - you hide the details of the implementation - you have here two > different implementations that look the same from the outside > - you package the related code in an entity - you don't have a bunch > of functions any more > > Jano > > ------=_Part_23897_17169718.1181748219871--