I recently started learning ruby and am confused between an instance variable and a local variable and a class variable. therefore, I recently wrote code that finds the largest palindrome in 1000 primes. the code:
def prime_large(number)
arr_prime = []
Prime.each(number) do |x|
new_arr_prime = arr_prime.push(x.to_s)
updated = new_arr_prime.select { |y| y.reverse == y }
end
p updated.max
end
p prime_large(1000)
The error I received is:
undefined local variable or method `updated' for main:Object (NameError)
I know that the updated local variable is prime, so I cannot access it outside of it, but I changed the code, replacing it with @updated, as shown below:
def prime_large(number)
arr_prime = []
Prime.each(number) do |x|
new_arr_prime = arr_prime.push(x.to_s)
@updated = new_arr_prime.select { |y| y.reverse == y }
end
p @updated.max
end
p prime_large(1000)
after changing it, I got the result:
"929"
"929"
in my code, without creating a class, how my instance variable (@updated) works. I am confused between local and instance variable. can someone explain the differences to me and how they work?