views:

30

answers:

2
def next_prime_number (last_known_prime)
  while true
    last_known_prime++

    found_factor = false # ERROR
    for i in 1...last_known_prime
      if last_known_prime % i == 0
        found_factor = true
        break
      end
    end
    if !found_factor
      puts "new prime: #{last_known_prime}"
      Kernel.exit
    end
  end
end

in `next_prime_number': undefined method `+@' for false:FalseClass (NoMethodError)

I'm getting the above error and am totally stumped. Any ideas (no, this is not homework, I am trying to teach myself Ruby through the Euler Project).

+3  A: 

There is no ++ operator for incrementing an integer in Ruby so try replacing last_known_prime++ with last_known_prime = last_known_prime + 1.

This will fix the error you're seeing. There is another problem with your program after that but I won't spoil your attempt to solve the Euler problem yourself.

mikej
Or last_known_prime += 1 or last_known_prime = last_known_prime.succ
Logan Capaldo
Thanks @Logan, I had a feeling there was a method like `.succ` but I couldn't remember its name off the top of my head.
mikej
+2  A: 

As mikej said, there is no post-increment (++) operator in ruby. There is however, a unary plus (spelled +@ when defining it)

last_known_prime++

found_factor = false

is getting parsed as something like

last_known_prime + (+(found_factor = false))

--------------------^ unary plus on false

which is causing your cryptic error.

Logan Capaldo