Anyway, there was a Stack Overflow link there which lead me to the solution and the correct usage of the prime module, if you’re running it locally and using a version newer than 1.9 (I’m on 2.3.3p222). If you’re also trying to run code locally and on a version newer than 1.9 change the following lines:
From:
Will have to dig around some to find the old posts on this topic. The prime module changed after 1.9, I believe, and the above can be done very simply now…
require 'prime'
def first_n_primes(n)
return "n must be an integer." unless n.is_a? Integer
return "n must be greater than 0." unless n > 0
Prime.first n
end
puts first_n_primes(10)
But that is moot. We cannot leave this topic listed since posting of solutions is frowned upon. Comparative analysis that others can discuss is not, though. Perhaps explain a bit further, or keep up your research and report differences that crop up with each new iteration of Ruby.
You’re welcome. I’m still unable to find the post where I had links and explanation for the brevity of that code. Will keep digging, since by my recall it gave some why’s and what-for’s. Wish my recall was so good I could give them now.