Ruby Variables
Leissan
Posted on October 3, 2022
In programming, a variable is basically a container which allows us to store information, something that holds a value.
Ruby has 5 different types of variables: as you might expect, there are local and global variables, there are instance and class variables, and there are constants. The way we can tell which is which is typically by using a special character at the beginning of the variable name. Let’s review them one by one.
Local Variables
A local variable name always starts with a lowercase letter(a-z) or underscore (_). These variables are local to the code block in which they are declared; their scope ranges from class, module, def, or do to the corresponding end or from a block's opening brace to its close brace {}. A local variable is only accessible locally, within the block of its initialization. Local variables are not available outside that block.
Local variables do not have any value (even nil) before initialization, which means you will encounter an error like the one below if you try to declare one without assigning its value:
The first assignment you make to a local variable works essentially like a declaration. When you are trying to reference an initialized local variable, Ruby interprets it as a call to a method that has no arguments (and we get the error like the one above).
Global Variables
Global Variables start with a dollar sign: $:
In our code snippet above, we are declaring the global variable on top level, outside of any class. When we test this code in IRB, we can see that this variable is available (or in scope) in both classes:
As we can see the scope of the global variable is such that it can be accessed across classes, from anywhere in your program, in other words – the scope is global. When a global variable is uninitialized, it has no value by default and its use is nil.
Instance Variables
An instance variable always starts with a @ sign. It is defined within the class body and belongs to a specific instance of that class. Its value is not shared between different instances of that class but rather it is local to specific instances of an object. Instance variables are available across methods for any specified instance or object i.e. instance variables can change from object to object. There is no need to initialize the instance variables and uninitialized instance variable always contains a nil value.
In the code snipper below, we created a class Toy, which has specific attributes like name and color. These attributes become our instance variables, with @ sign in front of them. As you can see, instance variables are declared inside the class.
Here we created two new instances of our Toy class: a red ball and a yellow truck. These attributes are describing not the entire class but only that specific instance (or instances) we created.
An instance variable belongs to the object itself (each object has its own instance variable of that particular class). One instance object can alter the values of its instance variables without modifying any other instance.
Class Variables
A class variable is a variable defined in a class body that can be interacted with without an instance of such class. Class variables are shared among all instances of the class and its subclasses. This means that only one variable value exists for all objects instantiated from this class. If one object instance changes the value of the variable, that new value will essentially change for all other object instances. It may make more sense once you review the code below:
Here, we have the same instance variables we reviewed above (@name, @color), as well as a class variable @@toy_count. The reason we are using a class variable here is because we need to keep track of data beyond just the instance level. Each instance of the toy has no idea how many other instances are there; it seems like the job is better suited for our class Toy.
The key practical difference between a class and an instance variable is that class variables (@@) are shared among a class and all of its descendants (like toy_count in our example), while instance variables (@) are not shared and each class has separate instance variables just like you would expect from different objects (like red ball and yellow truck).
Constant Variables
A constant is a type of variable which always starts with a capital letter. They can only be defined outside of methods, unless you use metaprogramming.
As long as you create a constant outside of any class, at the top-level of your code, that constant will be available anywhere, including inside class methods, outside the class, and in child classes.
Constants are used for values that aren’t supposed to change, but Ruby doesn’t prevent you from changing them, which could potentially be tricky for your code. This is why you wouldn’t typically use them very often in your code.
Posted on October 3, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.