undefined method 'chomp' for nil: NilClass

1

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

    
asked by anonymous 14.09.2015 / 04:29

1 answer

1

In the following section you ask the user to enter the amount of element, but then try to iterate over a String (undefined method 'for "1": String (NoMethodError)) , primeiro que você precisa converter essa string em inteiro, e depois que mesmo sendo inteiro para que haja iteração, esse objeto precisa ser um collection ( Array , Hash', etc)

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   

A small modification can solve:

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)
end    

The second excerpt has another error (undefined method each 'for 2: Fixnum (NoMethodError))' again you are trying to iterate over a non-iterable object, in this case a Fixnum (numeral). By observing you create an Array and instead of iterating over it, you transform it into number to iterate, in this case we use the Array itself and it is solved.

for j in array

After this we collide with another error: []': no implicit conversion of String into Integer (TypeError) You are doing integer comparisons with String, because the result of chomp is a String and in the array[j] <= 0 || array[j] < 40 excerpt you are doing comparison with numbers.

To solve this we return in the loop where we capture each user input and convert the input to integer.

n.times do |i|
    puts "Digite o valor #{i}"
    array.push(gets.chomp.to_i)
end   

So your code works:

Digite a quantidade de entradas: 
2
Digite o valor 0
1
Digite o valor 1
1
2
2

Final code would look something like:

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.to_i
array = Array.new

n.times do |i|
    puts "Digite o valor #{i}"
    array.push(gets.chomp.to_i)
end    

for j in array
    if array[j] <= 0 || array[j] < 40
        sequencia_numerica(array,j)
    end 
end
    
18.09.2015 / 16:21