Tag: 和操作员

Ruby中And / &&的运算符优先级

我对Ruby中的和/ && / =关键字有疑问。 ruby文档说,所提到的关键字的优先级是:(1)&&,(2)=,(3)和。 我有这段代码我写道: def f(n) n end if a = f(2) and b = f(4) then puts “1) #{a} #{b}” end if a = f(2) && b = f(4) then puts “2) #{a} #{b}” end 输出是: 1)2 4 [预期] 2)4 4 [ 为什么 ?] 出于某种原因,使用&&会导致a和b评估为4?