Reputation: 382
How come ().nil? is true in Ruby?
Upvotes: 4
Views: 264
Reputation: 11026
Playing in irb...
a = ()
a.class # => NilClass
a.nil? # => true
Upvotes: 2
Reputation: 132417
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
.
Upvotes: 9