#5 The Rubyist's Loop


#1

Can someone please help me figure out what is wrong with this code?
prime = Prime.new
n.times {
prime_array.push(prime.next)
}
end

this is the error message: It looks like the first_n_primes method no longer returns the correct value.


#2

You seem to have gotten rid of the implicit return here, probably by mistake. The method is supposed to return the array once it's built.


#3

Thank you for responding!
I have this now:

prime = Prime.new
n.times {
prime_array.push(prime.next)
}
end
prime_array
end

but the errors says: (ruby):17: syntax error, unexpected keyword_end, expecting $end


#4

Your return is supposed to be inside the method, not outside of it.


#5

I'm just not getting this one. I've tried a few different things but my current code looks like:

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

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

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

prime_array ||= []

prime = Prime.new
n.times
{prime_array.push(prime.next) }
prime_array
end

first_n_primes(10)

The error message: (ruby):13: syntax error, unexpected '}', expecting tASSOC


#6

got it.

prime = Prime.new
n.times {prime_array.push(prime.next)}
prime_array


#8

prime = Prime.new
n.times{prime_arry.push(prime.next)}
prime_array
end
first_n_primes(10)
do you guys know what I am doing wrong thats leading to an infinite loop I'm indenting like the video only to fail at seeing it


#9

Following is my code and its working fine.

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

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

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

prime_array ||= []

prime = Prime.new
n.times {prime_array.push(prime.next)}
prime_array
end

first_n_primes(10)


#10

what is the function of :
return prime_array ?
why you need this ?


#11

@amantourage
If you have a problem, please post this in a new thread with your question + code + error message


#12

I had the same problem, but I put spaces in the brackets, and it seems to work. n.times { prime_array.push(prime.next) }


#13

already way to go!
but in the console there was a problem..


#14

i got it. !
nevermind my question thank you :slight_smile: