Instance and Local Variables

Learn the difference between instance and local variables.

Instance variables

A careful reader would have already noticed the unknown prefix @ before the variable name. In Ruby, we can’t access variables out of the scope of the current method. The only exception is in the case of instance variables, which we’ll cover later. For example, the following Ruby code won’t be executed and will produce an error:

Get hands-on with 1200+ tech skills courses.