How to join the return of array

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

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

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 M. 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 15/10/2012, at 10:04 AM, Ruby M. [email protected] 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

Henry

Superb ! thnx ! I was mising on such a small concept :frowning:

Henry M. 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

Henry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs