Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Variable Types In Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 142
    Comment on it

    1.Global Variables
    2.Instance Variables


    1.Global Variables:

    •    They start with a $ sign.
    •    If we do not initialize values to global variables , they take a default value of  nil.

       

    Example:

    $ global_var = 20      #initialization of global variable
    class Global1
      def print_variable
        puts  Global Variable in Global1 is #$global_var
      end
    end
    
    class Global2
      def print_variable
        puts Global Variable in Global2 is #$global_var
      end
    end
    
    global1obj=global1.new
    globalobj.print_variable
    global2obj=Global2.new
    global2obj.print_variable

     

    Output:
    Global Variable in Global1 is 20      #This is the output from Global1 class
    Global Variable in Global2 is 20      #This is the output from Global2 class

    • As we can see in our example we have initialized the global variable only once and we are using it in multiple classes.
    • Global Variable provide us the ease of assigning it only once and use it anywhere in our code.
    • Say we you want to use the same value multiple times in your code , you can assign it to global variables which provides reusability.

    2.Instance Variables:

    •    They start with a @ sign.
    •    Like global variables if we do not initialize values to instance variables , they also take a default    value of nil.


    Example:

    class Instance
       def instance_variable(name, email, address)
          @ins_name=name               #instance variable for name
          @ins_email=email             #instance variable for email 
          @ins_address=address         #instance variable for address
       end
       def instance_display()
          puts "Person Name #@ins_name"
          puts "Person Email #@ins_email"
          puts "Person Address #@ins_address"
        end
    end
    
    # Creating Objects for instance variables
    inst1=Instance.new("Siddharth", "siddharthsindhi@gmail.com", "Dehradun")
    inst2=Instance.new("Gaurav", "gauravjhaldiyal@gmail.com", "Garhwal")
    
    # Calling Methods with instance variables
    inst1.instance_display()
    inst2.instance_display()

    Output:
    Person Name Siddharth
    Person Email siddharthsindhi@gmail.com
    Person Address Dehradun
    Person Name Gaurav
    Person Email gauravjhaldiyal@gmail.com
    Person Address Garhwal

    • As we can see in the example , we pass arguments to a function and then create the instance variables  to store the instance of those variables.
    • Then we create an object of that class to access the methods of that class and assign values to it's parameters , which are then stored in the instance variable.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: