I know joining arrays and strings but this issue is very specific to an
api I am working on :
All I want is all hq.close value in an array. EG:
[
{'GOOG' => [744.75,751.48,744.56,744.09,757.84]},
{'MSFT' => {value1,value2, .....}}
]
Reason I am not able to get the above array of hash is because when I do
puts hq.close its printing individual value and I am not sure how to get
all hq.close into one array
Based on code below my output is : (but i want it in above format)
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
on 2012-10-14 22:36
on 2012-10-14 22:50
out = Hash.new do {|h,v| h[v] = []}
YahooFinance::get_HistoricalQuotes( symb, Date.parse('2012-10-06'),
Date.today() ) do |hq|
puts "#{symb} -> #{hq.close}"
out[symb] << hq.close
end
Henry
on 2012-10-14 23:04
Thanks a bunch ! This works like a charm but just curious why I have to initialize and I could not just do out[symb] << hq.close without initialize Henry Maddocks wrote in post #1079818: > out = Hash.new do {|h,v| h[v] = []} > > YahooFinance::get_HistoricalQuotes( symb, Date.parse('2012-10-06'), > Date.today() ) do |hq| > puts "#{symb} -> #{hq.close}" > out[symb] << hq.close > end > > Henry
on 2012-10-14 23:10
On 15/10/2012, at 10:04 AM, Ruby Mania <lists@ruby-forum.com> wrote: > Thanks a bunch ! > This works like a charm but just curious why I have to initialize and > I could not just do > > out[symb] << hq.close > > without initialise Because the default value of an uninitialised hash key is nil and nil doesn't respond to << >> out = {} => {} >> out['foo'] => nil >> out['foo'] << 'bar' NoMethodError: undefined method `<<' for nil:NilClass from (irb):3 from /Users/henry/.rbenv/versions/1.9.3-p125/bin/irb:12:in `<main>' >> Henry
on 2012-10-14 23:19
Superb ! thnx ! I was mising on such a small concept :( Henry Maddocks wrote in post #1079820: > Because the default value of an uninitialised hash key is nil and nil > doesn't respond to << > > >>> out = {} > => {} >>> out['foo'] > => nil >>> out['foo'] << 'bar' > NoMethodError: undefined method `<<' for nil:NilClass > from (irb):3 > from /Users/henry/.rbenv/versions/1.9.3-p125/bin/irb:12:in `<main>' >>> > > > Henry
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.