secret
anonymous / kirillshchepelin.rb
Last active

  • Download Gist
kirillshchepelin.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
require "rexml/document"
include REXML
file = File.new("cia-1996.xml")
doc = REXML::Document.new file
 
# 1
greatest_population_country = XPath.match(doc, "//country").sort{|a,b| b.attributes["population"].to_i <=> a.attributes["population"].to_i}.first
 
puts "Population of the country with the most people: #{greatest_population_country.attributes["population"]}. And the country is #{greatest_population_country.attributes["name"]}\n\n"
 
# 2
puts "Five countries with the highest inflation rates: \n"
XPath.match(doc, "//country").sort{|a,b| b.attributes["inflation"].to_i <=> a.attributes["inflation"].to_i}[0..4].each_with_index do |country, index|
puts "#{index+1}. #{country.attributes["name"]} - #{country.attributes["inflation"]}\n"
end
 
puts "\n"
 
# 3
puts "List of continents and countries belongs to them. Everything in alphabetical order:\n"
XPath.match(doc, "//continent").collect{|a| a.attributes["name"]}.sort{|a,b| a<=>b}.each do |continent_name|
puts "#{continent_name}:"
XPath.match(doc, "//country[@continent = '#{continent_name}']").sort{|a,b| a.attributes["name"] <=> b.attributes["name"]}.each do |country|
puts country.attributes["name"]
end
puts "\n"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.