Undefined variable in Ruby – NameError: undefined local variable or method `a’ for #

Wondering why a variable is undefined in Ruby?

Getting an error like this? NameError: undefined local variable or method `a’ for #

1. Might be something to do with scope.

Check out these posts on Scope Gates (i.e. when you enter / leave a Class, Module or Method definition your scope changes):



2. Or you’ve referred to a local variable that doesn’t yet exist. E.g.

puts a

will raise a NameError exception in Ruby. But, 

a = 10

puts a

will not as the variable has been defined.

Leave a Reply

Your email address will not be published. Required fields are marked *