Sorry, this may be a bit of a noob question. This (economic.rb) is a script that parses some world economic data. I'm unsure how to pass the xml file to it. Normally, to run this I would do
ruby economic.rb
However, File.open is taking the ARGV[0] as a parameter. How do I pass the xml file (data.xml) into that when running the script.
economic.rb
require 'rubygems'
require 'nokogiri'
File.open(ARGV[0]) do |f|
xml_doc = Nokogiri::XML::Document.parse(f)
countries = xml_doc.css('country')
most_populous = countries.max_by {|node| node['population'].to_i}
puts "The most populous country in 1996 was #{most_populous['name']} with a population of #{most_populous['population']}"
puts
puts "The five countries with the highest inflation rate in 1996 were:"
countries.sort_by {|country| -(country['inflation'] || 0).to_f} [0..4].each do |country|
puts " #{country['name']} - #{country['inflation']}%"
end
continent_info = countries.group_by {|country| country['continent']}
puts
puts "The continents and their countries in 1996 were:"
continent_info.keys.sort.each do |continent|
continent_info[continent].sort_by {|country|
country['name']}.each do |country|
puts " #{country['name']}"
end
end
You can just run:
ruby economic.rb data.xml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With