2012-11-06 6 views
4

죄송합니다. 이것은 멍청한 질문 일 수 있습니다. 이 (economic.rb)는 일부 세계 경제 데이터를 구문 분석하는 스크립트입니다. XML 파일을 전달하는 방법을 모르겠습니다. 일반적으로이 작업을 수행하려면Ruby : File.open 파일을 인수로 전달하는 방법

ruby economic.rb 

그러나 File.open은 ARGV [0]을 매개 변수로 사용합니다. 스크립트를 실행할 때 xml 파일 (data.xml)을 전달하는 방법은 무엇입니까?

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 
+0

덕분에, 내가 ARGV를 시도 할 때 [1] 그것이 전무했다. 나는 args가 0에서 시작할 것을 짐작한다. – BrainLikeADullPencil

+0

아, 미안. ARGV [0]이 옳습니다. 'ruby economic.rb data.xml' 만 실행하면됩니다. – halfelf

+0

ARGV는 배열입니다. 루비 (그리고 다른 대부분의 언어들)의 배열은 0에서 시작합니다. –

답변

4

당신은 실행할 수 있습니다

ruby economic.rb data.xml 
관련 문제