开发者

How does Ruby evaluate math expressions to true/false?

I am going through the Ruby tutorials from Learn to Program, and I am having a problem with a while loop.

This is what I want to do:

while y_now % 4
    y_开发者_如何转开发now += 1
    puts y_now % 4
end
gets

I'm using the gets just to pause execution so I can see the output. (This is looking for a leap year, btw)

Anyways, when y_now % 4 = 0, the while loop continues, and I don't understand why, since this is different from all my previous experience.

I don't want someone to just fix my code though, I really want to understand why this happens. I've obviously just started Ruby, so whatever help I get is very much appreciated.

Note: This seems to be similar to this question, but with integers instead of strings.


In Ruby only the nil object and a special false object are "false", everything else (including the integer 0) is "true". You should use while (y_now % 4) != 0.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜