2 - To be or not to be

So, I don’t really know what the error is, because it says that the program took too long to finish, and that I should check for infinite loops.
$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 ||=

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

first_n_primes(10)

So, the only thing I changed was the prime_array ||= and I don’t see where the problem could be.

I tried again, and it worked.

Hey dude,

I don’t know exacly, but i think the,

prime_array =|| ,

means: prime_array could exist or not.
So if it exists. “don’t” create another, but if doesn’t, “New” prime_array.

Peace.

1 Like

Hey!
The answer is
$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 ||= []
  
  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end
first_n_primes(10)

Tnahks

i did the same.
still getting error

i’m very thanx for your code
the code is right

Ruby is very nit picky about spacing so you have to make its spaced out right. There should be a space right after prime_array and then another one after the equal sign.

prime_array ||=

[ask]
why you don’t define array ?
ex :
def prime_array (optional_array)
optional_array ||=
end