How to fill an array in Ruby?

Here is the code I'm working with:

class Trader

  def initialize(ticker ="GLD") 
    @ticker = ticker
  end

  def yahoo_data(days=12)

    require 'yahoofinance'

    YahooFinance::get_historical_quotes_days( @ticker, days ) do |row|
      puts "#{row.join(',')}"  # this is where a solution is required
    end
  end
end

The yahoo_data method gets data from Yahoo Finance and puts price history on the console. But instead of simple notes that evaporate into the air, how would you use the previous code to populate an array that can later be processed as an object.

Something along the lines of:

do |row| populate_an_array_method(row.join(',') end                                                                                                              
+3
source share
1 answer

If you do not give the block get_historical_quotes_days, you will get the array back. Then you can use mapto get an array of results join.

, ruby ​​1.8.7 , . , foo.bar {|x| puts x} 1,2,3, enum = foo.bar , 1,2,3. arr = foo.bar.to_a, [1,2,3].

, ( - , , ), foo.enum_for(:bar) , , bar.

, , get_historical_quotes_days , YahooFinance.enum_for(:get_historical_quotes_days).map {|row| row.join(",") }, , .

+4

Source: https://habr.com/ru/post/1783324/


All Articles