Javatpoint Logo
Javatpoint Logo

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
Ruby 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:

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:

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.
Next TopicRuby data types




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA