Ruby variable set to false but equal to zero

The assignment operator is as follows:

my_var = false 

And going to the next line, the debugger shows "my_var" with type = NilClass and value = nil! How is this possible?

+4
source share
3 answers

Did you inspect my_var directly without a debugger? The debugger may get confused or just show confusing results.

+3
source

It is possible that the target of your section of code has a "setter method", so it looks like you assign "my_var", but you actually call the method " my_var= ". See if your code has such definitions:

 def my_var=(x) # ... end 

If so, you will need to change the name of the local variable "my_var" or the setter method. You can also check by entering the line where you call " my_var = false ".

+2
source

I found this very useful True, False and Nil .

+1
source

All Articles