Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6746311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:18:27+00:00 2026-05-26T12:18:27+00:00

To begin, I am very new to Ruby on Rails. So I want to

  • 0

To begin, I am very new to Ruby on Rails.

So I want to get the data from this XML file in a hash so that the "currency" attribute of each "cube" is the key and the "rate" is the value.

First I tried using Hash.from_xml which got me the following, which looked even harder to work with:

 {"Envelope"=>{"Cube"=>{"Cube"=>{"Cube"=>[{"rate"=>"1.3627", "currency"=>"USD"}, {"rate"=>"106.58", "currency"=>"JPY"}, {"rate"=>"1.9558", "currency"=>"BGN"}, {"rate"=>"25.030", "currency"=>"CZK"}, {"rate"=>"7.4414", "currency"=>"DKK"}, {"rate"=>"0.85515", "currency"=>"GBP"}, {"rate"=>"309.48",
 "currency"=>"HUF"}, {"rate"=>"3.4528", "currency"=>"LTL"}, {"rate"=>"0.7037", "currency"=>"LVL"}, {"rate"=>"4.4774", "currency"=>"PLN"}, {"rate"=>"4.3528", "currency"=>"RON"}, {"rate"=>"9.0625", "currency"=>"SEK"}, {"rate"=>"1.2174", "currency"=>"CHF"}, {"rate"=>"7.7580", "currency"=>"NOK"}, {"r
ate"=>"7.5010", "currency"=>"HRK"}, {"rate"=>"42.1400", "currency"=>"RUB"}, {"rate"=>"2.4508", "currency"=>"TRY"}, {"rate"=>"1.3237", "currency"=>"AUD"}, {"rate"=>"2.3945", "currency"=>"BRL"}, {"rate"=>"1.3855", "currency"=>"CAD"}, {"rate"=>"8.6613", "currency"=>"CNY"}, {"rate"=>"10.5920", "curre
ncy"=>"HKD"}, {"rate"=>"12121.45", "currency"=>"IDR"}, {"rate"=>"5.0177", "currency"=>"ILS"}, {"rate"=>"67.1540", "currency"=>"INR"}, {"rate"=>"1529.33", "currency"=>"KRW"}, {"rate"=>"18.6867", "currency"=>"MXN"}, {"rate"=>"4.2525", "currency"=>"MYR"}, {"rate"=>"1.7152", "currency"=>"NZD"}, {"rat
e"=>"58.289", "currency"=>"PHP"}, {"rate"=>"1.7402", "currency"=>"SGD"}, {"rate"=>"42.162", "currency"=>"THB"}, {"rate"=>"11.1484", "currency"=>"ZAR"}], "time"=>"2011-11-01"}}, "subject"=>"Reference rates", "xmlns:gesmes"=>"http://www.gesmes.org/xml/2002-08-01", "Sender"=>{"name"=>"European Centr
al Bank"}, "xmlns"=>"http://www.ecb.int/vocabulary/2002-08-01/eurofxref"}}

So I ended up using ReXML:

xml = open("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")
doc = Document.new(xml)

a = []
b = []

doc.elements.each("//Cube") do |element|
  a << element.attributes["currency"]
  b << element.attributes["rate"]
end
currency_rates = {}
a.each_with_index {|k,i|currency_rates[k] = b[i]}

This code works, and I get the end result that I am looking for. But to me this seems convoluted to create two separate arrays and then melding them together, is there a better way to do this? Is there a way to create my hash in the block without the arrays?

Or maybe there is a better XML query I can do before my block? I know pretty much nothing about XPath.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:18:27+00:00Added an answer on May 26, 2026 at 12:18 pm

    Using your second example you can fill the hash directly without the two intermediate arrays:

    xml = open("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")
    doc = Document.new(xml)
    
    currency_rates = {}
    
    doc.elements.each("//Cube") do |element|
      currency_rates[element.attributes["currency"]] = element.attributes["rate"]
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To begin with, I must say that I am VERY new to Ruby on
I'm very new to Rails, so it's likely there's a simple concept here that
I want to get data from an API using Python. The API documentation give
I will begin this question by admitting I am very new to MVC. The
To begin i read all resources from xml in my MergedDictionaries and now I
I am very new to Triggers and so this is a very beginner question.
My question is very similar to Java: HTTP Post to create new "Product" in
Okay, first off I am very, very new to java. For this project I
I'm very new for trigger, now this what i was trying. I've two tables
I'm very new to sweave and I feel this will be an easy question,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.