Undefined `+ @ 'method for false: FalseClass (NoMethodError) ruby

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 get the above error and is completely at a dead end. Any ideas (no, this is not homework, I'm trying to teach myself Ruby through the Euler project).

+2
source share
2 answers

As maykey said, in ruby ​​there is no post-increment ( ++ ) operator. However, there is one unary plus (written +@ when it is defined)

 last_known_prime++ found_factor = false 

parsed as something like

 last_known_prime + (+(found_factor = false)) --------------------^ unary plus on false 

which causes your cryptic error.

+4
source

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 see. After that, there is another problem with your program, but I will not ruin your attempt to solve Euler's problem on my own.

+3
source

All Articles