Class: YahooStock::Result::HashFormat
- Inherits:
-
YahooStock::Result
- Object
- YahooStock::Result
- YahooStock::Result::HashFormat
- Defined in:
- lib/yahoo_stock/result/hash_format.rb
Overview
DESCRIPTION:
Convert results as an array of key value pairs
USAGE
YahooStock::Result::HashFormat.new(“data as string”)‘of’, ‘keys’].output The number of keys in the block array must be equal to the values expected to be returned
Mostly will be used as a separate strategy for formatting results
Instance Method Summary collapse
-
#initialize(data, stock_symbols, &block) ⇒ HashFormat
constructor
A new instance of HashFormat.
- #keys ⇒ Object
- #output ⇒ Object
Methods inherited from YahooStock::Result
Constructor Details
#initialize(data, stock_symbols, &block) ⇒ HashFormat
Returns a new instance of HashFormat.
12 13 14 15 16 17 |
# File 'lib/yahoo_stock/result/hash_format.rb', line 12 def initialize(data, stock_symbols, &block) @data = YahooStock::Result::ArrayFormat.new(data).output @stock_symbols = stock_symbols @keys = yield super(self) end |
Instance Method Details
#keys ⇒ Object
31 32 33 |
# File 'lib/yahoo_stock/result/hash_format.rb', line 31 def keys @keys.collect{|key| key.to_s.gsub(/\s/,'_').downcase.to_sym} end |
#output ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/yahoo_stock/result/hash_format.rb', line 19 def output data = Hash.new @data.each_with_index do |datum, index| row_values = {} datum.each_with_index do |item, i| row_values[keys[i]] = item end data[@stock_symbols[index]] = row_values end data end |