1
Good night to you all.
I’m learning Ruby and I’ve stumbled upon this error in the code I’m creating and I can’t go on or find the error. Can anyone tell me where the error is? ( I’m trying but by knowing little of 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 "Enter number of entries: " n = gets.chomp array = Array.new
for i in n puts "Enter the value #{i}" array.push(gets.chomp) end
for j in array.length if array[j] <= 0 || array[j] < 40 numeric sequence(array,j) end end
Updating
Thank you very much. I was able to complete the code by making some adjustments. It was of great help 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 "Enter number of entries: " n = gets.chomp.to_i array = Array.new
n. times do |i| puts "Enter the value #{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
What would be the
gets
of commandgets.chomp
? According to the documentation the methodchomp
is a method of string class.– iTSangar
gets reads a string from the default input (typed by the user) and returns a String.
– jose_castro_arnaud
Glad I could help! If the answer has been satisfactory you can elect it as an answer to help other members. Good studies.
– Luiz Carvalho