The Rubyist's Loop


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

What’s wrong whit this? Every time I hit “RUN” it start thinking and it keeps there…


#2

Please post a link to the exercise. Thanks.


#3

https://www.codecademy.com/courses/learn-ruby/lessons/the-refactor-factory/exercises/the-rubyists-loop?action=lesson_resume

Finally worked

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

first_n_primes(10)


#4

That was going to be my first suggestion.


#5

Yeah, tyvm, but that one was a changed version of the actual code that I did.
Tried many ways even without caps untill I got the “DO” in there.


#6

A block expression will work…

n.times {prime_array.push(prime.next)}

#7

It didn’t :P!
Had to go with “DO”


#8

Weird, the block expression worked for me. You should be done that unit by now. Quite a difference at the end, eh?


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.