Create a gist now

Instantly share code, notes, and snippets.

require 'rubygems'
require 'nokogiri'
cia_xml = Nokogiri::XML('cia-1996.xml'))
#a varaible for each data
avg_population =["",0]
avg_inflation = []
#loop troung all the xml nodes
cia_xml.root.xpath("country").each do |country|
#just a ordered hashmap for each country ;)
continents[country[:continent]]=[] if continents[country[:continent]] == nil
continents[country[:continent]] << country[:name]
#convert to integer for finest comparation
if (population = Integer(country[:population])) > avg_population[1]
#not all the countries have a inflation value then just return 0 to the array
avg_inflation << [country[:name],Float(country[:inflation] == nil ? 0 : country[:inflation])]
puts "The most populated country is #{avg_population[0]} with #{avg_population[1]} people"
puts "The five countries with the most higth inflation are :"
#sort the array by the second value of the sub-array
avg_inflation.sort!{|x,y| y[1] <=> x[1] }
#5 times buddy just 5 times
5.times do |i|
puts " #{avg_inflation[i][0]} with #{avg_inflation[i][1]}"
puts "The Contients and his countries"
#sort the continents
continents.to_a.sort!.each do |k,v|
#just print the name of the contient
puts k
#sort by alpha magic ruby and show it ! :D
v.sort!.each{|country| puts "\t #{country}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment