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 integer开发者_如何学JAVAs, as they should be, however this statement is not triggering the code block in the unless clause. Am I blatantly misusing the || operator?
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
You should be using a double equals (==
) for comparison in an if
or unless
clause, not a single equals (=
).
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.
精彩评论