Undefined method `chomp' for nil:Nilclass

Asked

Viewed 241 times

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 command gets.chomp ? According to the documentation the method chomp is a method of string class.

  • gets reads a string from the default input (typed by the user) and returns a String.

  • Glad I could help! If the answer has been satisfactory you can elect it as an answer to help other members. Good studies.

1 answer

1

In the following section you ask the user to enter the amount of element, but then try to iterate on a String (undefined methodeach' for "1":String (Nameserver)), 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 stretch we have another mistake (undefined methodeach' for 2:Fixnum (Nomethoderror) )` again you are trying to iterate on a noneternal object, in the case of a Fixnum (numeral). Watching you create an Array and instead of iterating over it, you turn it into a number to iterate, in this case we use the Array itself and it is solved.

for j in array

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

To resolve this we return to the loop in which we capture each user input and convert the input into integer.

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

That’s how your code works:

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

Final code would be 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.