2016-06-10 23 views
0

Ich versuche, die drei Werte in jedem Sub-Array zu extrahieren, aber es scheint nicht zu funktionieren. Gibt es etwas Ungewöhnliches an dem Objekt, das zurückgegeben wird, oder ist mein Array-Extraktionscode falsch?Extrahieren von Werten aus Array von Arrays

#https://github.com/Nedomas/indicators 
require 'active_support' 
require 'active_support/core_ext' 
require 'indicators' 

my_data = Indicators::Data.new([1,2,3,4,3,2,4,6,1,2]) 
temp=my_data.calc(:type => :bb, :params => 2) 
puts temp.inspect 
temp.output.each do |x| puts "#{x[0]},#{x[1]},#{x[2]}" end 

Ausgang

[email protected] ~/Desktop/_REPOS/misc/stock_analysis/forex/oanda/ruby $ ruby temp.rb 
#<Indicators::Main:0x00000002c1f9b0 @abbr="BB", @params=[2, 2], @output=[nil, [1.5, 2.914213562373095, 0.08578643762690485], [2.5, 3.914213562373095, 1.0857864376269049], [3.5, 4.914213562373095, 2.085786437626905], [3.5, 4.914213562373095, 2.085786437626905], [2.5, 3.914213562373095, 1.0857864376269049], [3.0, 5.82842712474619, 0.1715728752538097], [5.0, 7.82842712474619, 2.1715728752538097], [3.5, 10.571067811865476, -3.5710678118654755], [1.5, 2.914213562373095, 0.08578643762690485]]> 
temp.rb:9:in `block in <main>': undefined method `[]' for nil:NilClass (NoMethodError) 
    from temp.rb:9:in `each' 
    from temp.rb:9:in `<main>' 

Antwort

1

Nach Ihrer Ausgabe der erste Wert von output Variable ist nil Sie compact verwenden können:

Gibt eine Kopie von sich selbst mit allen Null-Elemente entfernt.

temp.output.compact.each do |x| 
    puts "#{x[0]},#{x[1]},#{x[2]}" 
end 
Verwandte Themen