Ruby Variables

Variable in ruby programming are used to store the value and reference of an Object's. There are five type of variable declaration are possible in ruby programming.

Local variable : var are used to define local variables. The scope of this variable is where it is defined. Normally it will be define within the block level and function level.

if 10 > 5
    #That is use within this block only
    status = "10 is bigger"
    puts status
end


def info(data)
    #data is local variable
    puts data
end

info("Method variable")
#data and status veriable are not
#Accessible outside the scope
10 is bigger
Method variable

When we need to use local variable value to outside the block then it will be possible to pass the variable value to method. Directly are not allowed to use local variable value to outside scope. There is possible to define local variable name are start to (_) underscore. And there can possible to define same name of variable in two different scopes.

def info(data)
    #data is local variable
    puts data
end
info("You are wrong")

if true
    #That is use within this block only
    data = "I am happy"
    puts data
end
You are wrong
I am happy

In this example there are define data name of variable in two different scope. Here variable name are same but there access scope are different.

Instance variable : @ are used to define instance variables. That is very important part of class when define instance variable of class and create one or than one objects of this class. Then In this situation each object are works in sperate instances variable of class.

class Record
    def set_data(data1,data2)
        @data1 = data1
        @data2 = data2
    end
    def display_data()
        puts "data1 : #{@data1}"
        puts "data2 : #{@data2}"
    end
end
#make objects of class
first = Record.new
second = Record.new
third = Record.new

#set instance variable value
first.set_data(true,false)
second.set_data("Ruby","2.7.0")
third.set_data(12,69.3) 

#Display instance variable values
puts "First obj"
first.display_data()

puts "Second obj"
second.display_data()

puts "Third obj"
third.display_data()
Class Instance Variable
First obj
data1 : true
data2 : false
Second obj
data1 : Ruby
data2 : 2.7.0
Third obj
data1 : 12
data2 : 69.3

Each instance of class are copying to all instance of variable class.

Class variable : @@ are used to define class variables. Class variable are use within the inherit class or within the class. This variable are share to class instance objects and class method. When if anyone are modify the variable stage then it will effect to all other which is capable to access this variable. Because class variable are create single copy.

class Record
    #class variable @@x
    @@x = 10
    
    def set_data(x)
        @@x = x
        #instance variable @y
        @y = @@x * @@x ; 
    end
    def class_var
        puts "Class variable : #{@@x}"  
    end
    def all_about
        puts "Class variable : #{@@x}"  
        puts "Instance variable : #{@y}" 
    end
end

testOne = Record.new
puts "Test One"
testOne.set_data(3)
testOne.all_about

testTwo = Record.new
puts "Test Two"
testTwo.set_data(4) #set new value
testTwo.all_about

puts "After class variable"
testOne.class_var
testTwo.class_var
Class Variable Example
Test One
Class variable : 3
Instance variable : 9
Test Two
Class variable : 4
Instance variable : 16
After class variable
Class variable : 4
Class variable : 4

This variable is need to set initial values before it will use. Otherwise compiler are produced an errors.

Global variable : $ are used to define global variable.

#local variable
data = "A"
def test
    #method local variable
    data = "B"
    puts data
end
test
puts data


#global variable
$info = 10

def test_two
    #modifed global variable
    $info = 20
    puts $info
end

test_two
puts $info
B
A
20
20

Constant variable : Use uppercase letter to define this variable.

PI = 3.141592 #constant variable

puts "#{PI}"

#PI = 3.33 #Not allowed to modified
3.141592

If not initialize any value to constant variable it will by default nil value.

Parallel Assignment

a,b = 1,2
p a
p b

a,b = ['a','b']
p a
p b

a,*b = ['x','y','z']
p a
p b

*a,b = ['P','Q','R']
p a
p b

*a,b = {'P'=>1,'Q'=>2,'R'=>3}
p a
p b

x = 2
#left to right assignment
a,b,c = x,(x+=2),(x+=3)
p a
p b
p c
1
2
"a"
"b"
"x"
["y", "z"]
["P", "Q"]
"R"
[]
{"P"=>1, "Q"=>2, "R"=>3}
2
4
7

Check if a ruby variable is defined

(defined? var) that are used to check variable is defined or not in a particular scope.

a = 10
c = nil
puts ((defined? a) == nil) ? "Not defined": "defined"

puts ((defined? b) == nil) ? "Not defined": "defined"

puts ((defined? c) == nil) ? "Not defined": "defined"
defined
Not defined
defined

Check variable type

Everything in ruby programming is form of an object's. So when we are check object data type then that is related to class.

def check_data(data)
    type = data==nil ? "nil" : data
    puts "Data : #{type}"
    puts data.class
    puts data.class.superclass
end
check_data(1)
check_data(1.2)
check_data(nil)
check_data("A")
check_data(true)
check_data(false)
check_data([1.2])
check_data(:symbol)
check_data({1=>"one"})
Data : 1
Fixnum
Integer
Data : 1.2
Float
Numeric
Data : nil
NilClass
Object
Data : A
String
Object
Data : true
TrueClass
Object
Data : false
FalseClass
Object
Data : [1.2]
Array
Object
Data : symbol
Symbol
Object
Data : {1=>"one"}
Hash
Object


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved