secret
anonymous / mattdahl.rb
Last active

  • Download Gist
mattdahl.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 28 29 30 31 32
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.