# Why need conditional assignment for prime_array in line 14

#1

I've passed this question. Still I have an issue
Why do I need to check whether prime_array is assigned yet or not? (line 14)
The prime_array is existed only in the function first_n_primes, right? This means after first_n_primes finished its task, prime_array will be deleted. So the next time using first_n_primes, prime_array will be brand new. Then why do I need to add ||= ?
Thanks.

``````\$VERBOSE = nil    # We'll explain this at the end of the lesson.
require 'prime'   # This is a module. We'll cover these soon!

def first_n_primes(n)

unless n.is_a? Integer
return "n must be an integer."
end

if n <= 0
return "n must be greater than 0."
end

prime_array ||= [] #line 14

prime = Prime.new
for num in (1..n)
prime_array.push(prime.next)
end
return prime_array
end

first_n_primes(10)``````

#2

`||=` is an alternative to `if` and you need to know if `prime_array` is `=` `[]`

#3

Thanks for your respond. Still, why do I need to know if prime_array is =[](or even be anything) before assigning it to be an array? Isn't it true that every time I call function first_n_prime, the prime_array isn't existed until line 14? It is because after the function first_n_prime finished its task, every variable created inside this function(except symbol, I'm not sure about this) will be deleted. Anyway, can you show me a case where prime_array ||=[] return a false value(which means prime_array was created before the ||= operation). To me, the ||= in line 14 will always be true, prime_array will always be assign as an (empty) array. I think the ||= operation is unnecessary, = operation is fine.

#4

I know that they didn't want to make the solution obvious, but I don't think providing the example they did helped understand what I was supposed to do.

The instructions pretty much said "Just replace the if with a ||=", but the example gave me the impression that there was more to it than that, and I was wracking my brain out as to why it didn't work. This forum helped a lot, but the example did not.