Chazu
Chazu

Reputation: 1018

Executing code if three conditions are false

I'm trying to skip over calculating some numbers when the result would be an attempt to insert NaN into the DB. My code is as follows:

unless @X = 0 || @Y = 0 || Z= 0 #Don't execute below code if any of the three values = 0
    #Do some stuff with @X, @Y and @Z
end

I know that X,Y and Z are positive integers, as they should be, however this statement is not triggering the code block in the unless clause. Am I blatantly misusing the || operator?

Upvotes: 1

Views: 488

Answers (3)

sawa
sawa

Reputation: 168131

Especially when you want to compare with zero, there is a built in command in ruby which is faster than doing == 0.

unless @x.zero? or @y.zero? or @z.zero?
  ...
end

You can use either || or or here.

Upvotes: 0

andrewmitchell
andrewmitchell

Reputation: 1619

You're using = the assignment operator. You want to be using == the equality operator. Your code should look like this:

unless @X == 0 || @Y == 0 || @Z == 0
...
end

Upvotes: 4

Dylan Markow
Dylan Markow

Reputation: 124429

You should be using a double equals (==) for comparison in an if or unless clause, not a single equals (=).

Upvotes: 2

Related Questions