Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: File.open how to pass file as arguments

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
like image 559
BrainLikeADullPencil Avatar asked Nov 06 '12 05:11

BrainLikeADullPencil


1 Answers

You can just run:

ruby economic.rb data.xml
like image 92
pje Avatar answered Oct 07 '22 10:10

pje