DEV Community

emmacohanim
emmacohanim

Posted on

Breaking Down Variable Types in Ruby

Introduction

There are four types of variables in Ruby: local variables, global variables, instance variables, and class variables. With so many variable types to choose from, it can be difficult to know which type should be used when assigning values to variables in Ruby code. Read on to learn about the behaviors and constraints of these different types of variables.

What are sigils?

Sigils are special characters attached to the beginning of some Ruby variables to indicate the variable's type. The two sigils you will encounter in Ruby code are @ and $. We use the @ sigil to identify instance variables and class variables. In Ruby code, @ indicates that the type of variable we have encountered is an instance variable, while the appearance of @@ indicates that we are dealing with a class variable. We can use the other sigil, $, to identify global variables in our code.

Local Variables

Local variables in Ruby are variables that are defined within a local scope. This scope can be within a class, module, or method. Local variables can begin with a lowercase letter or underscore. Because local are only defined in the local scope, these variables can only be used within their class, module, or method.

def my_method
    variable_type = "local"
    puts "This method accesses a #{variable_type} variable!"
end

Enter fullscreen mode Exit fullscreen mode

Global Variables

Global variables are variables that are defined within the global scope. These variables can be used and accessed anywhere within an application. This type of variable is preceded by a $. Global variables are convenient as they can be used to allow one class, method, or module in your application to easily access the value of a variable defined within another without the use of props. Although global variables may seem simple, they can result in unwanted complexities in your code and should therefore be used with caution.

def my_method
    $variable_type = "global"
end

def other_method_in_application
    puts "This method accesses a #{variable_type} variable!"
end
Enter fullscreen mode Exit fullscreen mode

Instance Variables

Instance variables are variables that act on a particular instance within a class. As mentioned above, these variables begin with the @ sigil. The example below illustrates the initialization of instance variables within a class.

class MyClass
    def initialize(instance_variable, other_instance_variable)
        @instance_variable = instance_variable
        @other_instance_variable = other_instance_variable
    end
end
Enter fullscreen mode Exit fullscreen mode

Class Variables

Class variables are variables that act on an entire ruby class. As described above, we can use two @ sigils to indicate that we are dealing with a class method. The example below demonstrates the initialization of class variables.

class MyClass
    def initialize
        @@class_variable = class_variable
        @@other_class_variable = other_class_variable
    end
end
Enter fullscreen mode Exit fullscreen mode

Conclusion

When deciding which type of variable to utilize in your Ruby program, it is important to consider two things. First of all, we must take into account which part(s) of our application will need to access our variable. Second, we must ask ourselves whether or not we would like the value of our variable to be consistent throughout our program. In general, it is best practice to avoid the use of global variables and instead utilize local variables. Additionally, sigils allow us to easily identify our variable types as global, instance, or class variables.

Top comments (0)