2/7 To be or not to be! Frustrating!

https://www.codecademy.com/courses/learn-ruby/lessons/the-refactor-factory/exercises/to-be-or-not-to-be?action=lesson_resume

Hello, i’m stuck on 2/7, To be or not to be, but i’m not sure i’m doing anything wrong…there is no error message coming up, but i still get a red cross every time i run. I’m sure the code is right and have checked it against many others on the forums, all of which seem to confirm the same code i have. Is it an issue with the site itself? Has anyone else had anything similar?



$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 ||= [] #this is the code, i have removed spaces after [].
  
  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end

first_n_primes(10)


many thanks

There is good possibility that support for the obsolete bits of Ruby is no longer present.

.new
.next

are both no longer supported in Ruby.

.new has been replaced with .instance which returns an iterable.

.next has been replaced with .succ

But none of this matters. It would be advisable at this time to read up on the Prime class to get yourself up to speed on the topic. For now though, this code will let you pass…

Remove all the code inside the method that is below the validation bits. Replace it with…

Prime.first n

So your code will look something like this…

require 'prime' 

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.first n
end

puts first_n_primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

That return line could be expanded upon, but it is moot since methods of an instance of the class can be invoked on the class itself.

prime = Prime.instance
prime_array = prime.first n
prime_array

Because of the drop in support (not CC’s doing) you might have to fudge your way through the remainder of the unit. The final lesson contains up to date code.

2 Likes

Thanks so much for the reply!

good news is that it got me past that stage, and then i did stage 3, but stage 4 asks to “remove unnecessary return” which has already been removed once i input your new code below the validation bits.

With nothing to remove, if i run the code, i get an infinte loop.


$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.first n
end

first_n_primes(10)

May be written implicitly…

“n must be an integer.” unless n.is_a? Integer

and same with this,

“n must be greater than 0.” unless n > 0

and we no longer need this line…

Will take a closer look and see where the hangup might be…

This works for lesson 4…

require 'prime'

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.first n
end

puts first_n_primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1 Like

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