In Ruby, I've just noticed that:
puts true and false
returnstrue
, whileputs (true and false)
andputs false and true
return bothfalse
.
What is the logic/reason behind this behaviour?
In Ruby, I've just noticed that:
puts true and false
returns true
, while puts (true and false)
and puts false and true
return both false
. What is the logic/reason behind this behaviour?
Because puts
binds stronger than and
: your code is equal to
(puts true) and false
true
#=> nil
You can check operators precedence in docs.
To get what you could use &&
, which has higher precedence than and
:
puts true && false
false
#=> nil