ilasno
ilasno

Reputation: 724

Ruby variable assigned false but is nil

The assignment statement looks like this:

my_var = false

And breaking on the very next line, the debugger shows 'my_var' with Type=NilClass and value=nil! How is this possible?

Upvotes: 4

Views: 570

Answers (3)

mu is too short
mu is too short

Reputation: 434965

Have you examined my_var directly without any debugger? The debugger could be getting confused or just displaying confusing results.

Upvotes: 3

maerics
maerics

Reputation: 156652

It's possible that the target object of your section of code has a "setter method" defined, so it looks like you're assigning to "my_var" but are actually calling the "my_var=" method. See if your code has any definitions like this:

def my_var=(x)
  # ...
end

If so, you'll need to change the name of the local "my_var" variable or the setter method. You could also verify by stepping into the line where you call "my_var = false".

Upvotes: 2

user483040
user483040

Reputation:

I found this very helpful True, False and Nil.

Upvotes: 1

Related Questions