Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does nil? work in ruby?

Tags:

ruby

How come ().nil? is true in Ruby?

like image 352
user114385 Avatar asked Dec 22 '22 06:12

user114385


1 Answers

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.

like image 82
Peter Avatar answered Jan 05 '23 14:01

Peter