Search code examples
rubyrubygemsyahoofinance

Using the Yahoo Finance Gem


I am trying to use the Yahoo Finance Gem, but am not able to get the information I want. When I try to get a quote, it creates a hash, but instead of the individual information (which I am trying to get), it gives a string will all the information in it. Is there a way to receive a single bit of information (such as % change) as a number? I am very new to ruby, so any help would be awesome.


Solution

  • require 'yahoofinance'
    YahooFinance.get_quotes(YahooFinance::StandardQuote, 'yhoo') {|i|
      puts i.change
      puts i.changePoints
      puts i.changePercent
      puts i.time
    }
    

    Prints for me:

    -0.03 - -0.17%
    -0.03
    -0.17
    10:55am
    

    or

    r = yahooFinance.get_quotes(YahooFinance::StandardQuote, 'yhoo')
    puts r[r.keys[0]].dayHigh
    puts r["YHOO"].dayHigh
    

    prints:

    17.43
    17.43
    

    YahooFinance.get_quotes return a hash in which quote symbols are keys, and all data for each quote is a value. See YahooFinance::BaseQuote class to guess why it is possible to use getters like dayHigh() to auto parse data from the hash value.