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
    • 184
    Comment on it

    There are 4 types of variables in Ruby: Global, Local, Instance and Class. Ruby is a language where a variable can contain any type of object, you can assign a string to a variable which has previously an integer value. As in ruby we generally don't declare variables, we need to define it with symbols whether it is of which type. Let's discuss one by one to each variables:

    1. Local Variables:

    These variables have local scope or you can say they are valid within the block or function in which they are defined. They don't have any prefix. Suppose for example.

    5.times do |n|
     puts n

    Here n is defined within a loop so it's scope will end when the loop ends. So n is a local variable here.

    2. Instance Variables:

    Instance variables belongs to one instance of a class. For defining instance variables you need to mention '@' symbol before the variable name or using self. They can be accessed from any instance of a class from within a method. You can access them using these methods:

    instance_variables    # Array of strings, listing instance variables
    remove_instance_variable  # Private method


    ## Defining instance variables
    @your_instance_var = 777
    ## using self
    self.your_instance_var = 777

    There are other ways to access these variables like using attr_accessor, attr_reader and attr_writer.

    3. Class Variables: 
    Class variable are those variables that are for the whole and also for its subclasses. So they are accessible from any instance of that class and if you modify it once place its value will be changed for every instance. To define instance variable you need to add two '@' symbols before your variable name (eg @@your_class_variable). Class variables are accessed like class method

    class A
      @@variable_name = 777
      def self.variable_name
    puts A.variable_name
    ## => 777
    class B < A
      @@variable_name = 999
    puts B.variable_name
    ## => 999
    puts A.variable_name
    ## => 999

    Thus you can see in the above example if we change the value of the class variable in the child class it's value automatically changes for the super class too. The different kinds of methods available for class variables are:

    class_variables    # Array of strings, listing class variables
    remove_class_variable  # Private method

    Class variables have some perhaps surprising behavior when you look at inheritance, as a value set in a subclass can affect other classes. Let us see that in action. SubClass1 has a parent, SuperClass, a sibling, SubClass2, and a child SubSubClass1.

    4. Global Variables:

    Global variables are accessible from anycase inside the application. If you change the value of global variables anywhere it automatically changes everywhere. They are defined by adding a dollar symbol before the variable (eg: $your_global_var).

    $global_var = 777

    To get the list of all the global variables:




 0 Comment(s)

Sign In

Sign up using

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: