Forum: Ruby Xml-fun and how do I make a method shorter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
64fe16be1acb2e31e2af4091b599685a?d=identicon&s=25 Kris Thomsen (kristho)
on 2009-04-15 20:03
I've got a little problem.

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?
4a5ad89e5a16e46dc7155fa8c6cee83c?d=identicon&s=25 Don Wood (tinnidril)
on 2009-04-15 20:16
Kris Thomsen wrote:
> I've got a little problem.
>
> 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
64fe16be1acb2e31e2af4091b599685a?d=identicon&s=25 Kris Thomsen (kristho)
on 2009-04-15 20:28
Don Wood wrote:
> Kris Thomsen wrote:
>> I've got a little problem.
>>
>> 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
This topic is locked and can not be replied to.