user727403
user727403

Reputation: 1977

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.

Upvotes: 2

Views: 2394

Answers (2)

boulder_ruby
boulder_ruby

Reputation: 39753

I'm running Rails 3.2.8 along with the Ruby 1.9.3 and was having some problems with this gem.

So I just went straight to the source code and took that one file (its just a single file, and short too) and placed it in my /lib folder. In case you haven't been using your lib folder, you must add something like config.autoload_paths += Dir["#{config.root}/lib/**/"] to config/application.rb in order to load up lib folder classes from the rail console or elsewhere in rails.

Besides, its probably the simplest source code you will find and its always good to start reading the actual source that you rely on every day.

Upvotes: 0

Alexander Gromnitsky
Alexander Gromnitsky

Reputation: 3029

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.

Upvotes: 1

Related Questions