Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • variables in ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 87
    Comment on it

    Variables in ruby Variables are used to hold any data which will be later used by programs. There are 5 types of variables in ruby

    1.Ruby global variables

    2.Ruby instance variables

    3.Ruby class varibles

    4.Ruby local variable

    5.Ruby Constants

    I will explain in detail about each variable Ruby global variable are used globally in whole application. Ruby global variable are denoted by $ sign

           $global_variable = 10
        class Demo
          def show_global
             puts "Global variable in Demo1 is #$global_variable"
          end
        end
        class Demo2
          def show_global
             puts "Global variable in Demo2 is #$global_variable"
          end
        end
    
        obj1 = Demo.new
        obj1.show_global
        obj2 = Demo2.new
        obj2.show_global
    
       Output:-
       Global variable in Demo1 is 10
        => nil 
       Global variable in Demo2 is 10
    
       

    Instance variable:-

    Instance variable are denoted by @. uninitialized instance variable produce nil value.

      class Customer
           def initialize(id)
              @cust_id=id
           end
           def display_details
              puts "Customer id #@cust_id"
            end
        end
    
        # Create Objects
        cust1=Customer.new("1")
        cust2=Customer.new("2")
    
        # Call Methods
        cust1.display_details
        cust2.display_details
    
      
    
          Output:-
          Customer id 1
            => nil    
          Customer id 2
            => nil 
      

    here @cust_id is instance variable Ruby class variable\ Class variables are used by the @@ and must be initialized before using in class

    class People
       @@no_of_people=0
       def initialize(id, name, addr)
          @cust_id=id
          @cust_name=name
          @cust_addr=addr
       end
       def display_details
          puts "Customer id #@custe @@no_of_customers in People
    _id"
          puts "Customer name #@cust_name"
          puts "Customer address #@cust_addr"
        end
        def total_no_of_people
           @@no_of_people += 1
           puts "Total number of people: #@@no_of_people"
        end
    end
    
    # Create Objects
    cust1=People.new("1", "John", "Apartments, Luda")
    cust2=People.new("2", "Poul", "Empire enclave, Kala")
    
    # Call Methods
    cust1.total_no_of_people
    cust2.total_no_of_people
    
      
      
     Total number of people: 1
      => nil 
     Total number of people: 2
    
     
    

    Ruby local variables

    Ruby local variables are started with using _ keyword, local variables are used within a scope of class, methods and modules.

      def local_variable
       a = 15
      end
      

    Ruby Constants

    In ruby Constants start with capital letter, ruby constants can be used with in a class where it has defined.

    
    class Example
       V1 = 100
       V2 = 200
       def show
           puts "Value of first Constant is #{V1}"
           puts "Value of second Constant is #{V2}"
       end
    end
    
    # Create Objects
    object=Example.new
    object.show
      
    
      Value of first Constant is 100
      Value of second Constant is 200
    
    

 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: