How is zero? work in ruby?

How come (). Nil? true in Ruby?

+4
source share
2 answers

The simple answer: () is an empty expression that evaluates to nil .

In more detail: all expressions have a result in Ruby, returning nil if there is nothing better to return. () does not cause any action on its own, so an expression that is just () returns nothing. Thus, the result of the expression is set to nil , and therefore ().nil? evaluates an empty expression, decides that nothing will be returned, returns nil . It really equals nil , so nil? says true .

+9
source

The game of irb ...

 a = () a.class # => NilClass a.nil? # => true 
+2
source

Source: https://habr.com/ru/post/1299603/


All Articles