# Code refactoring advice

**URL:** <https://www.ruby-forum.com/t/code-refactoring-advice/106540>\
**Category:** Ruby\
**Created:** [June 11, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/code-refactoring-advice/106540 "2007-06-11T20:10:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_TSHoover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nathan\_TSHoover](https://www.ruby-forum.com/u/Nathan_TSHoover)\
**Post date:** [June 11, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/code-refactoring-advice/106540/1 "2007-06-11T20:10:42Z")

</div>

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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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

Thanks,

Nathan

---

<div class="post-metadata">

**Author:** ![Nathan\_TSHoover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nathan\_TSHoover](https://www.ruby-forum.com/u/Nathan_TSHoover)\
**Post date:** [June 11, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/code-refactoring-advice/106540/2 "2007-06-11T22:20:45Z")

</div>

On 6/11/07, Nathan T.-Hoover [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘xml\_struct’  
> a.MyWrpWrp = {:xmlns =\> “mtvnCWWrpReq”}
> 
> ```
> a.MyWrpWrp.Wrp.WrpParms.Src.CustDefMsgSrc1
> 
> ```
> 
> }
> 
> 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 = “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Nathan\_TSHoover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nathan\_TSHoover](https://www.ruby-forum.com/u/Nathan_TSHoover)\
**Post date:** [June 11, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/code-refactoring-advice/106540/3 "2007-06-11T23:06:49Z")

</div>

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 = “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\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

---

<div class="post-metadata">

**Author:** ![Nathan\_TSHoover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nathan\_TSHoover](https://www.ruby-forum.com/u/Nathan_TSHoover)\
**Post date:** [June 12, 2007, 12:56am UTC](https://www.ruby-forum.com/t/code-refactoring-advice/106540/4 "2007-06-12T00:56:00Z")

</div>

On 6/11/07, Nathan T.-Hoover [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 = “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\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 = “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\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

---

<div class="post-metadata">

**Author:** ![Nathan\_TSHoover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nathan\_TSHoover](https://www.ruby-forum.com/u/Nathan_TSHoover)\
**Post date:** [June 13, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/code-refactoring-advice/106540/5 "2007-06-13T17:24:33Z")

</div>

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! 😃

- Nathan
