Skip to content

anonymous /mattdahl.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
# 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
Something went wrong with that request. Please try again.