# Xml-fun and how do I make a method shorter

**URL:** <https://www.ruby-forum.com/t/xml-fun-and-how-do-i-make-a-method-shorter/165619>\
**Category:** Ruby\
**Created:** [April 15, 2009, 8:03pm UTC](https://www.ruby-forum.com/t/xml-fun-and-how-do-i-make-a-method-shorter/165619 "2009-04-15T20:03:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e5b9ba/32.png) [@kristho](https://www.ruby-forum.com/u/kristho)\
**Post date:** [April 15, 2009, 8:03pm UTC](https://www.ruby-forum.com/t/xml-fun-and-how-do-i-make-a-method-shorter/165619/1 "2009-04-15T20:03:27Z")

</div>

I’ve got a little problem.

[http://pastie.org/447561](http://pastie.org/447561)

I want the parseFile-method to do what the outcommented-method do - just  
shorter and prettier.  
Anyone who can give some tips on how I can do this?

---

<div class="post-metadata">

**Author:** ![kristho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e5b9ba/32.png) [@kristho](https://www.ruby-forum.com/u/kristho)\
**Post date:** [April 15, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/xml-fun-and-how-do-i-make-a-method-shorter/165619/2 "2009-04-15T20:16:06Z")

</div>

Kris Thomsen wrote:

> I’ve got a little problem.
> 
> [http://pastie.org/447561](http://pastie.org/447561)
> 
> I want the parseFile-method to do what the outcommented-method do - just  
> shorter and prettier.  
> Anyone who can give some tips on how I can do this?

You have no clear return value, so I don’t know exactly how you want  
those arrays returned. I went ahead and put them in a Hash and returned  
that.

def parseFile file  
data= { ‘code’ =\> [], ‘desc’ =\> [], ‘rate’ =\> [] }  
file.search(‘currency’).each do |currency|  
data.each\_pair { |k,a| a.push currency.attributes[k] }  
end  
return data  
end

---

<div class="post-metadata">

**Author:** ![kristho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e5b9ba/32.png) [@kristho](https://www.ruby-forum.com/u/kristho)\
**Post date:** [April 15, 2009, 8:28pm UTC](https://www.ruby-forum.com/t/xml-fun-and-how-do-i-make-a-method-shorter/165619/3 "2009-04-15T20:28:55Z")

</div>

Don W. wrote:

> Kris Thomsen wrote:
> 
> > I’ve got a little problem.
> > 
> > [http://pastie.org/447561](http://pastie.org/447561)
> > 
> > I want the parseFile-method to do what the outcommented-method do - just  
> > shorter and prettier.  
> > Anyone who can give some tips on how I can do this?
> 
> You have no clear return value, so I don’t know exactly how you want  
> those arrays returned. I went ahead and put them in a Hash and returned  
> that.
> 
> def parseFile file  
> data= { ‘code’ =\> [], ‘desc’ =\> [], ‘rate’ =\> [] }  
> file.search(‘currency’).each do |currency|  
> data.each\_pair { |k,a| a.push currency.attributes[k] }  
> end  
> return data  
> end

Oh, pretty.  
But. How do I get the informations from my hashes? The idea was that I  
line the values up beside eachother. Like:

Code Desc. Rate  
EUR Euro 7.45  
USD American Dollars 5.55
