Skip to content

Instantly share code, notes, and snippets.

/mattdahl.rb Secret

Created September 2, 2010 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/580f37612e43abf2e9a5 to your computer and use it in GitHub Desktop.
Save anonymous/580f37612e43abf2e9a5 to your computer and use it in GitHub Desktop.
# To run this script make sure it is in the same directory as cia-1966.xml (http://rubylearning.com/data/cia-1996.zip)
# and simply run the file via command line (ruby mattdahl.rb)
# Also note you need the hpricot gem so if you don't have it run (sudo gem install hpricot)
require 'rubygems'
gem 'hpricot'
require 'hpricot'
file = File.open('cia-1996.xml', 'r')
doc = Hpricot(file)
continents = (doc/'continent')
countries = (doc/'country')
puts '1.'
country = countries.sort_by {|country| country.attributes['population'].to_i}.last
puts "\t#{country.attributes['name']} - #{country.attributes['population']}"
puts '2.'
countries.sort_by {|country| country.attributes['inflation'].to_f}.reverse[0..4].each do |country|
puts "\t#{country.attributes['name']} - #{country.attributes['inflation']}"
end
puts '3.'
countries.group_by {|country| country.attributes['continent']}.sort.each do |continent, countries|
puts "\t#{continent}"
countries.sort_by {|country| country.attributes['name']}.each do |country|
puts "\t\t#{country.attributes['name']}"
end
end
file.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment