开发者

Executing code if three conditions are false

开发者 https://www.devze.com 2023-03-06 18:51 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消