-1
def a
  puts 'a'
end

def b
  puts 'b'
end

p a || b #=> prints both 'a' and 'b' although it would only print a

p a && b #=> prints only a

这是怎么回事?我正在使用 1.9.3

编辑:我忘了 puts 返回 nil,现在这很有意义,感谢第一个答案:)

4

1 回答 1

3

没什么奇怪的,因为:puts方法返回nil,运算符的第二个参数&&没有被评估。但是,在第一种情况下,您会同时调用:a,然后调用:b,因为:a方法返回nil

于 2014-01-01T14:37:08.737 回答