开发者

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 informati开发者_如何学编程on 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜