-
Notifications
You must be signed in to change notification settings - Fork 2
Ruby error: undefined local variable or method
If you type a word into ruby, and it's not a keyword (e.g. if, else, def, class, etc.), ruby will first try to treat the word as a variable if it was previously defined, and if not, try to call a method using that word.
Ruby treats text between single or double-quotes as strings. Did you forget to put the text between quotes and now ruby thinks it's a variable or method?
For example, if I have the following code:
name = seanRuby would think that sean is a method or variable. What I really need is quotes around 'sean':
name = 'sean'/path/to/my_program.rb:30:in `my_method':
undefined local variable or method `my_variable_or_method_name'
for #<MyProgram:0x007fd229275440> (NameError)It expected that this code existed in the same method before it got to the error:
my_variable_or_method_name = "my variable value"`or that the method was defined in the same class:
def my_variable_or_method_name
# code...
endThis is a living document. If you have anything to add, change or remove, please let us know. Or better yet, as it's a wiki, make the change yourself!