Error Message: "Prime::new is obsolete"


#1

I am stuck on Codecademy, Learn Ruby, 7 Refactoring, The Refactor Factory, 7/7.
Here’s a link to the exercise: https://www.codecademy.com/courses/learn-ruby/lessons/the-refactor-factory/exercises/nice-work-2?action=lesson_resume

The Codecademy Ruby window won’t run any of the code for this exercise … I can’t even get Codecademy to run the initially provided code for 1/7. It seems to stall here. I initially discovered this in Google Chrome and the I’m having the same issue with Firefox. I recall that Codecademy just updated their Ruby course … could this change be the source of my problem? I couldn’t get Codecademy to work, so I tried things out in my desktop interactive Ruby and also my command line and I still have problems. The error I receive is "Prime::new is obsolete. use Prime::instance or class methods of Prime. Replacing Prime.new from line 26 of my code doesn’t fix the problem; it causes another problem. The “next” from line 29 of my code “n.times {prime_array << (prime.next) }” isn’t a method … my command line spits out "undefined method ‘next’ for #Prime:0x2563b60 .

Here is my code with my comments:

=begin
"The Refactor Factory" project is the concluding exercise for the Learn Ruby unit 7 track called "Refactoring."
This work is part of Codecademy's Full Stack Web Developer Program that I am working on.
This exercise provides you with poorly written Ruby code for a program that generates a list of prime numbers 
up to the entered ending number.
=end


require 'prime'   #This pulls in the Ruby prime module.

#first_n_primes is our program name and it accepts the integer n, which it's called with at the end of the program.
def first_n_primes(n)
#is_a? Integer is used to check if the submitted number is an integer. An error is reported otherwise.
#The original code had a 3 line unless statement here that I've updated to be a single-line if statement.
return "n must be an integer." unless n.is_a? Integer

#Prime numbers are integers that are non-negative, so an error is thrown if a positive # isn't submitted.
#I've replaced the three line if statement from the initial version with this single-line if statement.
return "n must be greater than 0." if n <= 0

  #Here the variable prime_array is defined as an array if it hasn't already been defined to be something
  #See the original code, file with "initial" tagline instead of "refactored," for the inclusion
  #of the "if prime_array.nil?" Here I use the conditional assignment operator to simplify that code.
  prime_array ||= [] 
  #
  prime = Prime.instance
  #There was an uncessary for loop here. I've replaced it with a simpler n.times.
  #.push can be simplified with the concatenation operator (<<).
  n.times {prime_array << (prime.next) }
  prime_array
  #There was a "return prime_array" that I removed. It wasn't necessary because the prime_array was just evaluated.
end

first_n_primes(10)

Thanks for any help you can provide :smiley:


#2

If you remember, we had that weird-looking $VERBOSE = nil on line 1; this allowed us to use the old-style Prime.new from Ruby 1.8 without the interpreter yelling at us.

require 'prime'

def first_n_primes(n)
  # Check for correct input!
  "n must be an integer" unless n.is_a? Integer
  "n must be greater than 0" if n <= 0

  # The Ruby 1.9 Prime class makes the array automatically!
  prime = Prime.instance
  prime.first n
end

puts first_n_primes(10)

#3

Thank you! This code works :smiley:

It’s not important, but I still have a question about the old format of code I was trying to write. I’ve added the “$VERBOSE = nil” to my initial code and gone back to the old Prime.new method. Nothing happens when I try to run this older-style code … do you happen to know why? I assumed that adding the $VERBONE=nil line would allow this older-style code to run.


$VERBOSE = nil
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_array ||= []
prime = Prime.new
n.times {prime_array << (prime.next) }
prime_array
end
first_n_primes(10)



#4

Do you mean the code would not run? Or that you didn’t get any squawk from the interpreter?


#5

I hadn’t thought of that! How do I determine whether or not the code runs?

I’ve added a screenshot of my Command Prompt after trying to run the old and new code. The old code is named “the-refactor-factory-initial.rb” and the new code is called “the-refactor-factory-refactored.rb.” The old code with the “initial” at the end of the file name doesn’t have any output when run.

unrefactored ruby doesn't run


#6

That could be because (and I’m only guessing) the old support is removed for the final lesson and only the new support (Ruby 1.9+) is present. The old code did work in the earlier lessons so that is why I draw this conclusion. I think it is safe to move on. Bookmark that last lesson as NB - breaking new ground.


#7

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