문제

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

What's going on? I'm using 1.9.3

Edit: I forgot puts returns nil, now it makes sense, thanks to the first answer :)

도움이 되었습니까?

해결책

Nothing strange, since :puts method returns nil, the second argument of && operator didn't evaluated. However, in first case you've get the both call to :a, and then to :b, because :a method returned nil.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top