Ruby Variables

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby Variables

Ruby variables are locations which hold data to be used in the programs. Each variable has a different name. These variable names are based on some naming conventions. Unlike other programming languages, there is no need to declare a variable in Ruby. A prefix is needed to indicate it.

There are four types of variables in Ruby:

  • Local variables
  • Class variables
  • Instance variables
  • Global variables

Local variables

A local variable name starts with a lowercase letter or underscore (_). It is only accessible or have its scope within the block of its initialization. Once the code block completes, variable has no scope.

When uninitialized local variables are called, they are interpreted as call to a method that has no arguments.


Class variables

A class variable name starts with @@ sign. They need to be initialized before use. A class variable belongs to the whole class and can be accessible from anywhere inside the class. If the value will be changed at one instance, it will be changed at every instance.

A class variable is shared by all the descendents of the class. An uninitialized class variable will result in an error.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. class States   
  4.    @@no_of_states=0   
  5.    def initialize(name)   
  6.       @states_name=name   
  7.       @@no_of_states += 1   
  8.    end   
  9.    def display()   
  10.      puts "State name #@state_name"   
  11.     end   
  12.     def total_no_of_states()   
  13.        puts "Total number of states written: #@@no_of_states"   
  14.     end   
  15. end   
  16.   
  17. # Create Objects   
  18. first=States.new("Assam")   
  19. second=States.new("Meghalaya")   
  20. third=States.new("Maharashtra")   
  21. fourth=States.new("Pondicherry")   
  22.   
  23. # Call Methods   
  24. first.total_no_of_states()   
  25. second.total_no_of_states()   
  26. third.total_no_of_states()   
  27. fourth.total_no_of_states()  

In the above example, @@no_of_states is a class variable.

Output:

Ruby variables 1

Instance variables

An instance variable name starts with a @ sign. It belongs to one instance of the class and can be accessed from any instance of the class within a method. They only have limited access to a particular instance of a class.

They don't need to be initialize. An uninitialized instance variable will have a nil value.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. class States   
  4.    def initialize(name)   
  5.       @states_name=name   
  6.    end   
  7.    def display()   
  8.       puts "States name #@states_name"   
  9.     end   
  10. end   
  11.   
  12. # Create Objects   
  13. first=States.new("Assam")   
  14. second=States.new("Meghalaya")   
  15. third=States.new("Maharashtra")   
  16. fourth=States.new("Pondicherry")   
  17.   
  18. # Call Methods   
  19. first.display()   
  20. second.display()   
  21. third.display()   
  22. fourth.display()  

In the above example, @states_name is the instance variable

Output:

Ruby variables 2

Global variables

A global variable name starts with a $ sign. Its scope is globally, means it can be accessed from any where in a program.

An uninitialized global variable will have a nil value. It is advised not to use them as they make programs cryptic and complex.

There are a number of predefined global variables in Ruby.

Example:

In the above example, @states_name is the instance variable.

Output:

Ruby variables 3

Summary

  Local Global Instance Class
Scope Limited within the block of initialization. Its scope is globally. It belongs to one instance of a class. Limited to the whole class in which they are created.
Naming Starts with a lowercase letter or underscore (_). Starts with a $ sign. Starts with an @ sign. Starts with an @@ sign.
Initialization No need to initialize. An uninitialized local variable is interpreted as methods with no arguments. No need to initialize. An uninitialized global variable will have a nil value. No need to initialize. An uninitialized instance variable will have a nil value. They need to be initialized before use. An uninitialized global variable results in an error.