- 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…
i = 5
puts i # result = 5
i = 7.7
puts i # result = 7.7
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
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?
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 .
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
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
1234 def self.sides@@sidesend
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
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