5

抱歉,这可能是一个菜鸟问题。这个 (economic.rb) 是一个解析一些世界经济数据的脚本。我不确定如何将 xml 文件传递​​给它。通常,要运行它,我会这样做

ruby economic.rb

但是, File.open 将 ARGV[0] 作为参数。运行脚本时如何将 xml 文件 ( data.xml ) 传递到该文件中。

经济.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
4

1 回答 1

5

你可以运行:

ruby economic.rb data.xml
于 2012-11-06T05:16:50.410 回答