I’m trying to write a script that will take stock ticker symbols from a
file, pull the company name and current price and write it back to the
file while ignoring comments and blank lines so as to leave the rest of
the file intact.
I can get the stock prices etc. i’m after using the yahoofinance module
like so
require ‘rubygems’
require ‘yahoofinance’
quote_type = YahooFinance::StandardQuote
Set the symbols for which we want to retrieve quotes.
quote_symbols = [‘yhoo’,‘goog’]
Get the quotes from Yahoo! Finance. The get_quotes method call
returns a Hash containing one quote object of type “quote_type” for
each symbol in “quote_symbols”. If a block is given, it will be
called with the quote object (as in the example below).
YahooFinance.get_quotes( quote_type, quote_symbols ) do |qt|
puts “QUOTING: #{qt.symbol}”
puts “#{qt.name}”
puts “#{qt.lastTrade}”
end
I want to create an array of ticker symbols like quote_symbols above,
but from a file.
here’s a test data file i’m using
this is the comment line
that was a blank, below is a stock
YHOO
GOOG # this comment can be deleted
i would like the output to look like
this is the comment line
that was a blank, below is a stock
YHOO:Yahoo! Inc.:17.35
GOOG:Google Inc.:567.49
this is the code i’m having trouble with.
filename = ARGV[0]
File.open(filename, ‘r+’) do |f|
lines = f.readlines # load array of lines
lines.each do |line|
next if line =~ /^#/ # skip comments
next if line.chomp.empty? # skip empty lines
$ticker[lines] = line #***the problem
puts lines.index(line)
end
end
$ticker.each do |p|
puts “#{p}”
end
*** I’m trying to create the array $ticker that has all my stocks so i
can pass it on to the yahoofinance module.
The other stumbling block for me is how to keep the results of YHOO →
Yahoo! Inc. and the price 17.35 matched up with YHOO so i can put it
back on the same line.
Thanks
Peter