How come ().nil? is true in Ruby?
Simple answer: ()
is an empty expression that evaluates to nil
.
More detailed: all expressions have a result in Ruby, returning nil
if there's nothing better to return. ()
doesn't cause any action by itself, so an expression that is merely ()
has nothing in particular to return. Thus the result of the expression is set to nil
, and so ().nil?
evaluates an empty expression, decides there's nothing much to return so returns nil
. This is indeed equal to nil
, so nil?
says true
.
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