Skip to main content

CHAPTER 3 : - TYPES OF VARIABLES

tTypes Of Variables In Ruby 



There are five types of variables in ruby all of them are discussed below:-


 
Variable Scope      Name Begin With
 1} Constants Variable [ A - Z ]  
 2} Local Variable [ a - z ]  or _(underscore)
 3} Global Variable $ ( Doller )
 4} Instance Variable @ (at)
 5} Class Variable @@ ( double at  ) 

we will look at all those one by one.

Constant Variable:-

This is the only Language that allows a constant value to changed value under the execution. The name of these variables is in all capital letters. 

Example:-

                                                                CONSTANT = "hello"

Ruby Local Variable:-

Local variables are local to the code construct in which they are declared. For example, a local variable declared in a method or within a loop cannot be accessed outside of that loop or method. These variable names start with small letters and underscore.

Example:-  
                     a) test_num = 2
                     b) _test = "hello world"

Ruby Global Variable:-

Global variables in ruby are accessible from anywhere in the ruby program, regardless of where they are declared. Global variable names are prefixed with a dollar sign($).

The use of global variables is strongly discouraged. The problem with global variable is that, not only are they visible anywhere in the code for a program, they can also be changed from anywhere in the application, This can make tracking bugs difficult.

Ruby Class Variable:-

A class variable that is shared amongst all instances of a class. This means that only one variable value exists for all objects instantiated from this class. This means that if one object instance changes the value of the variable, that new value will essentially be changed in all instances.

Ruby Instance Variable:-

An instance variable has a name beginning with @, and its scope is confined to whatever object self refers to. ... Instance variables of ruby do not need declaration. This implies a flexible structure of objects. In fact, each instance variable is dynamically appended to an object when it is first referenced. check here

Example:-  
                                                a) @ test_num = 2
                                                b) @test = "hello world"


conclusion:-

in the end, I just wanna say that all types of variables are gonna help you a lot when we move to learn the rails framework so just grasp the concept carefully and precise.



Comments

Popular posts from this blog

CHAPTER 1 :- RUBY OPERATORS

A Quick Guide To Ruby Operators As we work with any other language and getting started with any other language we are so used to get through the various fundamentals of that particular language. One of these fundamentals are the operators that are used in that language so that we can apply logic to our code and add certain functionality to our application and through this, we can make our application more towards the solution of the problem, so let as us check out how many types of operators we have in Ruby programing language. Many of them are listed below Types Of Operators: Arithmetic operators Logical operators Comparison Operators Assignment Operators Bitwise Operators Ternary Operator Range Operators defined? Operator Dot"." and Double Colon"::" Operators RubySplat Operator Matching Operator = = and = = = Summary  Now let us have a brief look at all the forms of operators Arithmetic Operators -:      The arithmetic operators are used to perform arithmetic oper...

CHAPTER 2 :- CONDITIONAL STATEMENTS

The Control Flow Statements  As we move deeper into the programing and logic building we recognize a high demand to control the flow of the program rather than shift the flow of the program based on the type of inputs and various types of results that are obtained through various calculations. For instance, let us assume we want to check whether a person is allowed to vote or he/she is prohibited. To check this we have to get the age of the person as an input and process the input to conditional operations so that we can allow or deny a person  to solve this we can just use the if statement. But what if we need to process a humungous amount of information at a time and shift the flow of control accordingly so to get us out of these problems Ruby provides a verity of Control Flow Statements. if statements if-else-end statements if-elsif-else-end statements switch-when statements unless inline unless and if state...