You really understand variables in Ruby

Tram Ho

1. What is a variable?

Variables are where data is stored. Each variable has its own name, the variable name is set according to some specific rules. Each variable has its own data type. Ruby has many data types available. The data type in Ruby is a dynamic data type, that is, when we declare a variable and assign a value, Ruby will automatically assign the data type to the variable based on that value, but we do not need to declare the data type in advance. data like in languages like C++, Java…

Eg :

2. Variable Types in Ruby

In ruby there are 5 types of variables defined:

❖ Global variable – symbol $
➔ Available everywhere in your Ruby script
❖ Local variable how to make variable is lowercase letter
➔ It depends on scope (create new scope with new class, new module, method)
❖ Instance variable – symbol @
➔ Available only in a specific object, across all methods in a class
Not available directly from class definitions.
❖ Class variable – symbol @@
➔ Available from the class definition and any subclasses. Not available from anywhere outside.
❖ Constants – Start with a capital letter or capitalize the entire variable name. Constant variables in ruby can change values.
As you can see, there are many different types of variables in ruby, so you should not consider using them appropriately

3 . How to use variables in Ruby

Global variables

Through the above example, it is easy to see that: $global is a variable that is not in class C, but when we call the new object to the my_method method, the $global variable will take the initial value of 0 and then add 1. Of course is the variable value $global = 1 now . That’s easy to understand, isn’t it?

Local variable

Read the local variable’s scope again and guess how? I’m sure you guessed it right.
Looking at the above code, the variable has only 1 variable, which is color (easy to use), so whether it has the same value or not. When we call the method function, what value is printed “Red” or 192. color = “Red” This variable is in the scope of the method function, so it does not print and color = “Red” will print in puts because its scope is outside the function .


Instance variable

To understand that better than this variable, I will refer to the class. If you haven’t read the class in Ruby, don’t worry too much. I just made its scope ok. The sooner you know something, the better…

  • In Ruby class is defined

The initialize function is a constructor in ruby.

The variable  student_id , student_name  is an instance variable, so it only has scope within the class method. Used when instantiating the student object

Class variable

At this point, you must have imagined what a class in Ruby is like, right? So continue to learn more types of variables @@

Maybe people are weird about this function

This function is named **class method** . Simply when the class calls this function without instantiating an object. Because the *self* keyword is referring to the class itself that calls it. It’s very interesting to learn self in Ruby

Constant variable

It’s easy to see if the NAME constant variable is correct.

I mentioned that constants are mutable, that’s cool in Ruby.
So in ruby is there a way to not change the constant known value. Reply : okay, using the freeze keyword used in (array, string is an array of strings) will not change the contanst variable:

  • Recently, I have introduced about variables in Ruby and when to use those types of variables. Hope the article I share can help newbies to learn Ruby understand variables and interesting things in Ruby. It’s my first time blogging, so please leave a comment so I can improve. See you again for a few more posts to share
Share the news now