开发者

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=NilClas开发者_运维知识库s and value=nil! How is this possible?


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


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".


I found this very helpful True, False and Nil.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜