我正在写一个程序,允许用户输入日期并输入他们在特定日期所需的股票信息的类型。所有的股票信息和日期都在一个单独的CSV文件中。我的程序不会运行,我觉得我好像缺少了一些东西。这是一个类,我不想使用Ruby的CSV类。我如何查看GE股票信息?
下面是这个文件看起来什么样,如:
date,open,high,low,close,volume,changed,changep,adjclose,tradeval,tradevol
2013-10-07,23.84,23.90,23.80,23.89,3522559,-0.16,-0.67%,23.89,83992937.36,8462
2013-10-04,24.18,24.18,23.90,24.05,33274615,-0.05,-0.21%,24.05,800232596.05,74361
2013-10-03,24.22,24.25,23.84,24.10,37466161,-0.23,-0.95%,24.10,902130194.02,95122
这是我的计划:
#open the file
data = File.open("data.csv","r+")
#make an empty hash
stocks = {}
contents = data.readlines
data.close
#this add quotes between each line
contents.collect! do |x|
x.chomp
end
#this splits up each in into its own array
contents.collect! do |x|
x.split(',')
end
contents.each do |x|
stocks[x[0]] = x
end
puts "This program has all the General Electic stock information from November 27, 1960 to October 8 2013. Please enter the date you would like to find the stock information of like this: 1997-10-30 (year-month-day)."
#prompt user for the date of the stock info they would like to find
date = gets.chomp
data = stocks[date]
puts "Please enter what information about the stock you would like to know: open, high, low, close, volume, changed, percent change,adjusted closing, trade value, or trade volume. Please put a underscore in place of all spaces."
#get an input for what type of stock information the user would like
input = gets.chomp
#elsif statement to give the user the info they need based on what stock info they want
if input == open
puts "The open of your stock is: #{data[1]}"
elsif input == high
puts "The high of your stock is: #{data[2]}"
elsif input == low
puts "The low of your stock is: #{data[3]}"
elsif input == close
puts "The close of your stock is: #{data[4]}"
elsif input == volume
puts "The volume of your stock is: #{data[5]}"
elsif input == changed
puts "The volume of your stock is: #{data[6]}"
elsif input == percent_change
puts "The percent change of your stock is: #{data[7]}"
elsif input == adjusted_closing
puts "The open adjusted closing of your stock is: #{data[8]}"
elsif input == trade_value
puts "The trade value of your stock is: #{data[9]}"
else input == trade_volume
puts "The trade volume of your stock is: #{data[10]}"
end
这是对您的问题代码进行的彻底编辑,使答案看起来不相关。 –