Recently, I observed a very interesting result in Ruby while making use of && and & for the input combination of 0 & 1.
Can someone please explain the below output with respect to the above mentioned two operators? The below is implemented using Ruby 2.0.0-p451
2.0.0-p451 :006 > 0 && 1
=> 1
2.0.0-p451 :008 > 0 & 1
=> 0
Thank you
&&
is the logical AND operator. It will be truthy, IFF both operands are truthy. And it is lazy (aka short-circuiting), which means it will stop evaluating as soon as the result has been fully determined. (So, since both operands need to be truthy, if the first operand is falsy, you already know that the result is going to be falsy, without even evaluating the second operand.) It will also not just return true
or false
, but rather return the operand which determines the outcome. IOW: if a
is falsy, it'll return a
, otherwise it'll return b
:
nil && (loop {})
# => nil
# Note: the infinite loop is *not* evaluated
# Note: the return value is nil, not false
true && nil
# => nil
true && 'Hello'
# => 'Hello'
&
simply calls the method &
. It will do whatever the object wants it to do:
def (weirdo = Object.new).&(other)
puts 'Whoah, weird!'
'Hello, ' + other
end
weirdo & 'World'
# Whoah, weird!
# => 'Hello, World'
In general, &
and its brother |
are expected to perform conjunction and disjunction. So, for booleans, they are perform AND and OR (TrueClass#&
, FalseClass#&
, NilClass#&
, TrueClass#|
, FalseClass#|
, NilClass#|
) with the exception that &
and |
are standard method calls and thus always evaluate their argument and that they always return true
or false
and not their arguments.
For Set
s, they perform set intersection and set union: Set#&
, Set#|
. For other collections (specifically Array
s), they also perform set operations: Array#&
, Array#|
.
For Integer
s, they perform BITWISE-AND of the two-complement's binary representation: Fixnum#&
, Bignum#&
, Fixnum#|
, Bignum#|
.
&&
is a boolean and
. It returns the second argument if the first argument is true-ish. Because 0
is true-ish in Ruby, 1
is returned.
&
is a bitwise and
. It compares the bit representation of the values. Because (imaging 8 bit) 00000000
(0
) and 00000001
(1
) have no 1
digits in common, 00000000
(0
) is returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With