7
有時候,以下會導致奇怪的錯誤:未定義的局部變量或爲主要方法'':對象
> nil || 4
NameError: undefined local variable or method ` 4' for main:Object
> nil || []
NameError: undefined local variable or method ` ' for main:Object
的原因是什麼?
有時候,以下會導致奇怪的錯誤:未定義的局部變量或爲主要方法'':對象
> nil || 4
NameError: undefined local variable or method ` 4' for main:Object
> nil || []
NameError: undefined local variable or method ` ' for main:Object
的原因是什麼?
你不小心你的Mac,其產生的non-breaking space上鍵入Alt鍵 + 空間。 Ruby不認爲這是空格,而是作爲變量名的一部分。然後它抱怨該變量不存在。
Some people like to remap Alt + Space to Space in order to prevent this typo.
我個人喜歡在我的文本編輯器來突出不可見的字符,所以我立刻注意到這個錯誤。
好主意,Alt + Space確實會導致這樣的錯誤。它也作爲應用程序的一部分出現,也許源代碼也包含一個Alt +空格。是否有可能以某種方式使其可見? – 0x4a6f4672 2014-11-05 12:26:23
'grep''-R .'(在單引號之間鍵入alt +空格) – 2014-11-05 12:28:16
謝謝。這是一個棘手的錯誤,你需要在Mac上按下「Alt」來產生一個「|」,至少在德語鍵盤上。很容易在下面的空格中鍵入「Alt + Space」。 – 0x4a6f4672 2014-11-05 12:48:10