Introduction
Coming from Javascript background, I found ruby variable declaration a bit simple. Simple in a bad way. I was introduced to declaring Ruby variables as:
variable_name = [1,2,3,4]
This may seem great. After all, there is no need to "memorize more syntax"; however, issues arise when code gets more complex.
Issues
When it comes to scoping. How was I sure that my variable was local and not global? Well easy enough right? I just had to make sure that I declared the variable outside a function to make it global and inside a function to make it local.
As I am writing this, I am realizing that the issues I have with ruby variable declarations is less functional and more syntactic. I would like to have a way to declare a variable and also the type of variable.
What I learned
As I was reading documentation for Classes and Objects in Ruby, I felt as if an angel had answered my prayers. There is more to meets the eye when it comes to declaring variables in Ruby.
In order to understand some of these variables you must be familiar with classes and that is what my next post is about. I will introduce them here and explain them more in depth on my next post.
1.Local variables
Local variables is what I was initially introduced to. To declare a local variable, you have two options. Declare the lowercase variable name within the block you want to access it in or place an underscore before the variable name.
_var_name = 1
2.Instance variables
Instance variables are limited to what an object itself refers to and different objects (class instances) of the same Class are allowed to have different values for the instance variable. It is declared with an @ prior to the variable name. Does this look familiar when using erb?
@var_name = 2
3.Class variables
A class variable is a static variable that persists throughout each instance of a class. If it gets modified in an instance it is modified in all instances. In order to declare this variable, you write @@ prior to your variable name.
@@var_name = 3
4.Global variables
Although declaring global variables is frowned upon for clouding the global namespace and causing unintended bugs. There is also a way to declare global variables in Ruby. Kind of similar to declaring local variables, if you want to declare a global variable just declare the lowercase variable name in the global scope. Luckily, we can also prepend the $ sign to visually see that it is a global variable.
$var_name = 4
Conclusion
Ruby variable declaration is not as simple as I thought. Understanding these different variable declarations have helped me understand the need for an @ sign in some of my previous code; as well as, understand Classes in a deeper level.
Top comments (1)
Yes, as we move to Rails, you will discover that, similar to Java, we will be using the old objects in objects thing: objects that have their own instance variables, references to other objects, most if not all derived from standard classes. The database wrapper objects will be globally available. There will be few if no $user_defined_global variables.