Skip to content

Instantly share code, notes, and snippets.

@luisvm
Created September 9, 2010 17:47
Show Gist options
  • Save luisvm/572220 to your computer and use it in GitHub Desktop.
Save luisvm/572220 to your computer and use it in GitHub Desktop.
require 'rexml/document'
xml = File.read('cia-1996.xml')
doc = REXML::Document.new(xml)
continents = []
countries = []
doc.elements.each('cia/continent') do |continent|
continents << {:name => continent.attribute('name').to_s}
end
doc.elements.each('cia/country') do |country|
countries << {:name => country.attribute('name').to_s, :inflation => country.attribute('inflation').to_s.to_i, :population => country.attribute('population').to_s.to_i, :continent => country.attribute('continent').to_s}
end
countries.sort!{|a,b| b[:population]<=>a[:population]}
p "1. " << [countries.first[:name], countries.first[:population]].join(' with ')
countries.sort!{|a,b| b[:inflation]<=>a[:inflation]}
p "2. "
0.upto(4){ |rank| p [countries[rank][:name], countries[rank][:inflation]].join(' with ')}
p "3. "
continents.sort{ |a,b| a[:name] <=> b[:name] }.each{ |continent|
p "=====> Continent: #{continent[:name]} <========"
countries.select{ |country| country[:continent] == continent[:name] }.sort{ |a,b| a[:name] <=> b[:name] }.each{ |country| p country[:name] }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment