1
votes

So I am running Ruby 1.9.3 and I am trying to use the Prime class.
I have added require mathn at the top of my .rb file

Other than the method name, this code came from the documentation page:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/prime/rdoc/Prime.html

def prime_number(n)
  Prime.each(n) do |prime|
    p prime 
  end
end  

prime_number(100)  

Any ideas why this doesn't work? The error I get says

undefined method each for Prime:Class

2
Hmm. Works for me as long as I do the require. - lurker
It has to work. Prime probably messed up somewehere between require and your method. Tried in irb: require 'mathn' # true Prime.respond_to? :each # true - Torimus

2 Answers

2
votes

I tried it in Ruby 1.9.3 and it worked. Tried it again in Ruby 1.8.7 and got your error message. A newer version of Ruby will probably solve your problem.

0
votes

Your problem is that you did require mathn and you must have require 'mathn' at the top of your file.

Either that, or you have neglected the quotes when you described your code above.