Good night everyone.
I'm learning Ruby and I came across this error in the code I'm creating and can not continue or find the error. Can someone tell me where the error is? :( I'm trying but knowing little ruby I still can not understand why the code did not interpret.
def sequencia_numerica(array, posicao)
if posicao == 1
puts 2
elsif posicao == 2
puts 3
else puts 2 * array[posicao - 1] + 3 * array[posicao - 2]
end
end
puts "Digite a quantidade de entradas: "
n = gets.chomp
array = Array.new
for i in n
puts "Digite o valor #{i}"
array.push(gets.chomp)
end
for j in array.length
if array[j] <= 0 || array[j] < 40
sequencia_numerica(array,j)
end
end
Update
Luiz thank you very much. I was able to complete the code by making some adjustments. It was a great help to the teachings, as I am learning these details were essential to correct the mistakes.
def sequencia_numerica(array, posicao)
if posicao == 0
array[posicao] = 2
return 2
elsif posicao == 1
array[posicao] = 3
return 3
else return 2 * array[posicao - 1] + 3 * array[posicao - 2]
end
end
puts "Digite a quantidade de entradas: "
n = gets.chomp.to_i
array = Array.new
n.times do |i|
puts "Digite o valor #{i}"
array.push(gets.chomp.to_i)
end
n.times do |i|
if array[i] <= 0 || array[i] < 40
puts sequencia_numerica(array,i)
end
end