
Variables
When I first learned about variables, it was somewhat confusing. A variable is pointing to a value but it is not the value. In fact, the variables are “storage locations” for data. They are a way of naming information for later usage.
In Ruby it’s quite easy to declare a variable, you just write the name of the variable and set it equal to a value.
learn_to_code = "Flatiron"
or
web = 0216
You noticed that I separated the words that compose the variable name with underscores, this is the Ruby convention for local variables.
It seems all great, you have a variable name that points to a value so that you can reuse the value later. So I can just use the variable name and get access to the data. Great!!
When you start programming, you have no idea (at least, I had no idea) how anything works. I had no idea that a computer program could contain thousand, ten thousand or even a million lines of code organized in many different folders. By now I understand that programs are very structured and divided into small blocks of code that perform different tasks. They themselves are contained in other bigger block of codes. Those blocks of code rely on each other, give back some info that other blocks can use. It’s like dividing the work into small tasks.
I talk about that because our variables are not accessible everywhere. Yes, we couldn’t talk about variables without talking about scope.
Variable Scope
Scope refers to the visibility of variables. In other words, which parts of your program can see or use your variable.
For example:
def feeling
love = "I love Ruby"
end
“love” is a local variable, it cannot be seen or used outside of the method “feeling”. If you call it outside you get an undefined local variable error. And if there was another variable outside the method “feeling” it could not be seen or used from the inside.
With every block, method or class comes a scope. Because of that, there are different types of variable and different ways to name them.
Here are the different types of variables:
Local Variables:
They start with a lowercase letter or an underscore and consist of letters, underscores, and/or digits.
Ruby convention is to use underscores rather than camel case when composing local variable names from multiple words.
For example ‘the_flatiron_school’ instead of ‘theFlatironSchool’.
Instance Variables:
Are used to store information for individual objects. They always start with a single at sign ( @ ). It follows the same rule as the local variable after the @ sign except that the first letter after the @ sign can be uppercase. The Ruby convention is that the first letter after the @ sign is lowercase.
For example ‘@the_flatiron_school’
Class Variables:
They store information that can be seen in the entire class. They follow the same rule as the Instance variable except that they start with two at signs (@@)
For example ‘@@the_flatiron_school’
Global Variables:
The global variable can be seen and used from everywhere. They start with a dollar sign ( $ ) and don’t follow the same rules as the other variables. In fact, it seems that there is no rule for what comes after the dollar sign. It could be $: or $/ or $4%-# but the convention in Ruby seems to be to have the word following the dollar sign in capital letter.
For example ‘$THE_FLATIRON_SCHOOL’
Constants:
They begin with an uppercase letter. The Ruby convention is to use either camel case (TheFlatironScool) or all uppercase words when composing constant names from multiple words (THE_FLATIRON_SCHOOL).
- Local Variables: the_flatiron_school
- Instance Variables: @the_flatiron_school
- Class Variables: @@the_flatiron_school
- Global Variables: $THE_FLATIRON_SCHOOL
- Constants: THE_FLATIRON_SCHOOL