0

I am trying to extract the three values in each sub array, but it doesnt seem to work. Is there something unusual about the object that is returned, or is my array extraction code incorrect?

#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

output

king@death-star ~/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>'

2 Answers 2

1

According to your output the first value of output variable is nil, you can use compact:

Returns a copy of self with all nil elements removed.

temp.output.compact.each do |x| 
   puts "#{x[0]},#{x[1]},#{x[2]}"
end
Sign up to request clarification or add additional context in comments.

Comments

1

First element of your array of array is nil and nil has no method []. You can use compact method to remove nil elements.

http://docs.ruby-lang.org/en/2.0.0/Array.html#method-i-compact

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.