From: Nathan Taylor-Hoover Date: 2007-06-12T06:05:28+09:00 Subject: Re: Code refactoring advice ------=_Part_124849_15671023.1181595929050 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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. #!/usr/bin/ruby require 'rubygems' require 'xml_struct' require 'uuidtools' XMLVER = "\n" def createXmlRequest() 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.MsgUUID="#{msguuid}" request.BigWrapR.MyParm.SrcID request.BigWrapR.MyParm.Athing request.BigWrapR.Verg.VergParms request.BigWrapR.Verg.VergParms.ApplID request.BigWrapR.Verg.VergParms.VergID request.BigWrapR.Verg.VergParms.VergVer request.BigWrapR.Verg.VergParms.RqstUUID="#{rqstuuid}" request.BigWrapR.Verg.VergParms.RoutingID request.BigWrapR.Verg.VergParms.Src.CustDefMsgSrc1 request.BigWrapR.Verg.Stuff.MoreStuff request.BigWrapR.Verg.Stuff.MoreStuff.Tag request.BigWrapR.Verg.Stuff.MoreStuff.AnTag request.BigWrapR.Verg.MsgData return request end def modNameSpace(request, namestring) request.BigWrapR = {:xmlns => "#{namestring}"} end request = createXmlRequest() modNameSpace(request, "newNameSpace") puts XMLVER + request.to_s On 6/11/07, Rick DeNatale wrote: > > On 6/11/07, Nathan Taylor-Hoover wrote: > > I have some code written that basically looks like below. I want to > > design a Module, Method, Class, Whatever to make each xml component > > accessible by a method and the ability to set attribs, params, clear > > params, attribs, etc. I know how to implement the code to do all of > > this, but I don't have any experience in created object oriented > > implementations (background is all scripting, and some C so used to > > making functions). Would appreciate any advice on how to make this OO. > > > > #1/usr/bin/ruby > >> require 'rubygems' > > require 'xml_struct' > > require 'uuidtools' > > > > setuprequest = lambda { |a| > > > > XMLVER = "\n" > > > > msguuid = UUID.random_create.to_s > > > > rqstuuid = UUID.random_create.to_s > > > > a.MyWrpWrp = {:xmlns => "mtvnCWWrpReq"} > > > > a.MyWrpWrp.MyWrpVer="1.0" > > > > a.MyWrpWrp.MsgUUID="#{msguuid}" > > > > a.MyWrpWrp.YoYoParms.SrcID > > > > a.MyWrpWrp.YoYoParms.TestInd > > > > a.MyWrpWrp.Wrp.WrpParms > > > > a.MyWrpWrp.Wrp.WrpParms.ApplID > > > > a.MyWrpWrp.Wrp.WrpParms.WrpID > > > > a.MyWrpWrp.Wrp.WrpParms.WrpVer > > > > a.MyWrpWrp.Wrp.WrpParms.RqstUUID="#{rqstuuid}" > > > > a.MyWrpWrp.Wrp.WrpParms.RoutingID > > > > a.MyWrpWrp.Wrp.WrpParms.Src.CustDefMsgSrc1 > > > > a.MyWrpWrp.Wrp.BigWrap.LilWrap > > > > a.MyWrpWrp.Wrp.BigWrap.LilWrap.Mom > > > > a.MyWrpWrp.Wrp.BigWrap.LilWrap.Dad > > > > a.MyWrpWrp.Wrp.MsgData > > > > return a > > } > > > > request = XmlStruct.new > > > > setuprequest.call(request) > > > > puts request.to_s > > Not that this makes it any more object oriented, but why use a lambda? > Sure it works but in this case I find that it obfuscates a bit why > not something like?: > > equire 'rubygems' > require 'xml_struct' > require 'uuidtools' > > def setuprequest(request=XMLStruct.new) > XMLVER = "\n" > msguuid = UUID.random_create.to_s > rqstuuid = UUID.random_create.to_s > request.MyWrpWrp = {:xmlns => "mtvnCWWrpReq"} > # etc > > return request # or more idiomatically just have request as the last > line. > } > > # Now since we provided a default parameter, and we are returning the > # request the usage simplifies to: > > puts setuprequest.to_s > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ > > ------=_Part_124849_15671023.1181595929050--