Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined local variable or method ` ' for main:Object

Sometimes, the following causes a strange bug:

> nil || 4
NameError: undefined local variable or method ` 4' for main:Object

> nil || []
NameError: undefined local variable or method ` ' for main:Object

What is the reason?

like image 236
0x4a6f4672 Avatar asked Nov 05 '14 12:11

0x4a6f4672


1 Answers

You are accidentally typing Alt + Space on your Mac, which produces a non-breaking space. Ruby does not consider this as whitespace, but as part of a variable name. It then complains that the variable does not exist.

Some people like to remap Alt + Space to Space in order to prevent this typo.

I personally like to highlight invisible characters in my text editor, so I notice the typo immediately.

like image 155
Patrick Oscity Avatar answered Sep 23 '22 18:09

Patrick Oscity