问题
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).
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/3548701/undefined-method-for-falsefalseclass-nomethoderror-ruby