Ruby variables


variable is a capacity area that holds any information that can be utilized by any program.

Ruby bolsters five sorts of factors.

  • General lowercase letters, starting with an underscore: (Variable).
  • $ toward the starting: Global variable.
  • @ toward the starting: Instance variable.
  • @@At the starting: Class variable class factors are shared all through the legacy chain
  • Capital letter starts with: Constant.

You've just observed these factors in the past sections, and this part will walk you through the five kinds of factors in detail.

Ruby worldwide variables

Global factors begin with $. The estimation of a uninitialized worldwide variable is nil, which is cautioned when the - w alternative is utilized.

Assigning a worldwide variable changes the worldwide state, so worldwide factors are not prescribed.

The following precedent demonstrates the use of worldwide factors.


#!/usr/bin/ruby # -*- coding: UTF-8 -*- $global_variable = 10 class Class1 def print_global puts "Global variables are output in Class1 as #$global_variable" end end class Class2 def print_global puts "Global variables are output in Class2 as #$global_variable" end end class1obj = Class1.new class1obj.print_global class2obj = Class2.new class2obj.print_global

Here, $global_variable is a global variable. This will produce the following results:

Note: In Ruby, you can access the value of any variable or constant by placing a # character in front of a variable or constant.

Global Variables Class1< /span> The output is   10
Global Variables Class2 Medium output is 10

Ruby instance variables

Instance variables start with @. The value of an uninitialized instance variable is nil, which is warned when the -w option is used.

The example below shows the usage of instance variables.


#!/usr/bin/ruby class Customer def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end end # Create object cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # Call method cust1.display_details() cust2.display_details()

Here, @cust_id, @cust_name, and @cust_addr are instance variables. This will produce the following results:

Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala

Ruby class variable

Class factors start with @@ and must be instated before they can be utilized in strategy definitions.

Referencing a uninitialized class variable will produce a blunder. A class variable can be shared and utilized in a subclass or submodule of a class or module that characterizes it.

Overloading a class variable produces a notice when the - w choice is utilized.

The following model demonstrates the utilization of class factors.

Here, @@no_of_customers is a class variable. This will deliver the accompanying results:

Total number of customers: 1 

Total number of customers: 2

Ruby neighborhood variables

Local factors start with a lowercase letter or an underscore _. The extent of a neighborhood variable is from class, module, def, or do to the comparing end or from the left support to the correct prop {}.

When a uninitialized neighborhood variable is called, it is deciphered as calling a technique without any contentions.

Assigning a uninitialized nearby factor can likewise be considered as a variable statement. The variable will endure until the finish of the present field. The existence cycle of a nearby factor is dictated by the Ruby parser.

In the above precedent, the neighborhood factors are id, name, and addr.

Here, VAR1 and VAR2 are constants. This will create the accompanying results:

The estimation of the first constant 100 

The estimation of the second constant 200

Ruby pseudo-variables

They are uncommon factors that resemble nearby factors, however carry on like constants. You can't relegate any qualities to these factors.

  • self: The sink object for the present technique.
  • true: An esteem that speaks to genuine.
  • false: speaks to the estimation of false.
  • nil: speaks to the estimation of vague.
  • __FILE__: The name of the present source document.
  • __LINE__: The quantity of the present line in the source document.

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.