Steph Lopez


Write to me.
Newb to Newb: Variable Scope
How variables talk to each other in Ruby.
02.01.2015.

Variable scope is simply a variable's visibility throughout the code. DBC likes to refer to scope as levels of confidentiality as depending on the type of variable you use, it may not be accessible anywhere in the program. The Ruby variables we've been learning this week include local, instance, class, global, and constant. This illustration may be helpful to reference as you read about the different scopes- you can also think about each level as being associated with the indentation levels of your program.

Local variables
Considered the most confidential and narrow scope of variables, local variables are the most common and begin with a lowercase letter. Once you go outside of the local variable's indentation level, it's no longer available to reference- it "goes out of scope". I found that when I made the mistake of trying to use the variable outside of scope, my reader instantly raises an error message that reads "undefined local variable or method". To avoid this, make sure to assign a value to your local variable before trying to use it.
Remember: A good way to know when to use a local variable would be to ask yourself if the information you are storing is temporary.

Instance variables
Denoted with an @ symbol in front of their name, instance variables exist within the context of an instance. The main difference between an instance variable and a local variable is that they aren't confined to any particular scope. Within the class, you can refer to the instance variable in any method.
Remember: Are you storing data that represents the state of the object? If the answer is yes, and this isn't for temporary storage, an instance variable is a good choice. An example I reference is a student's name, age, grades, etc.

Class variables
Inside the class, but outside of any methods is where you'll find your class variables. This type of variable needs to be initialized at the beginning of your class using two @ characters (@@). Once defined, the class variable will be shared amongst all instances of a class.
Remember: If you decide to change the value of your class variable, it will change across all other object instances within the class.

Global variables
The least used due to their unpopularity amongst Rubyist (they will actually call the use of globals as very "un-ruby"), global variables are accessible anywhere in your program. Similarly, and to their fault, they can be changed by anyone as well. You'll recognize them by their $ character in front or if they happen to be one of the commonly used predefined globals including $*, $@, $~, and $0. I haven't had much experience with these yet, but see the syntax in Stack Overflow boards often.
Remember: Think twice before using this type of variable.

Constant variables
Similar to local and class variables, constants can only be assigned once. It's like the value of pi, or other widely accepted values. These guys stand out in their ALL_CAPS denotation.
Remember: If you have to change a constant, there are workarounds. A simple Google search will turn out some options for you.