0

我知道加入数组和字符串,但这个问题对于我正在处理的 api 来说非常具体:

我想要的只是数组中的所有 hq.close 值。例如:

[
 {'GOOG' => [744.75,751.48,744.56,744.09,757.84]},
 {'MSFT' => {value1,value2, .....}}
]

我无法获得上述哈希数组的原因是因为当我将 hq.close 放入其打印单个值时,我不确定如何将所有 hq.close 放入一个数组中

根据下面的代码,我的输出是行分隔的:(但我希望它采用上述格式)

GOOG -> 744.75
GOOG -> 751.48
GOOG -> 744.56
GOOG -> 744.09
GOOG -> 757.84
MSFT -> 29.2
MSFT -> 28.95
MSFT -> 28.98
MSFT -> 29.28
MSFT -> 29.78

+++++++++++++++++++++++++++++++代码:

require 'yahoofinance'
#Stock symbols
user_input = ['GOOG','MSFT']

user_input.each do|symb|
 YahooFinance::get_HistoricalQuotes( symb,
                                     Date.parse('2012-10-06'),
                                     Date.today()) do |hq|
   puts "#{symb} -> #{hq.close}"
  end
end
4

1 回答 1

2

您可以注入每个值来构建您的哈希:

require 'yahoofinance'

company_symbols = ['GOOG','MSFT']
start_date = Date.parse('2012-10-06')
stop_date = Date.today()

company_symbols.inject({}) do |memo, value|
  memo[value] ||= []
  YahooFinance.get_HistoricalQuotes(value, start_date, stop_date) do |hq|
    memo[value] << hq.close
  end
  memo
end
# => {"GOOG"=>[744.75, 751.48, 744.56, 744.09, 757.84], "MSFT"=>[29.2, 28.95, 28.98, 29.28, 29.78]}
于 2012-10-14T21:37:01.870 回答