2007年2月14日 星期三

Ruby:and, &&, or, ||的差別

有些重要觀念讀過後就懂了,也沒必要記到Wiki上,記到Wiki上被看到的機率也較低,還是寫在Blog上好了。

FAQ 6.10 What’s the difference between “or'’ and “||'’?

Q: “p(nil || “Hello”)'’ prints “Hello”, while “p(nil or “Hello”)'’ gives a parse error.

A: || combines terms within an expression. Because the first term in this case is nil, the second term is evaluated.

or is used to combine expressions in conditionals. Ruby is not expecting a conditional statement in an argument list.

結合terms時要用&&、||,結合expression時要用and、or。

沒有留言:

張貼留言