Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ruby variable


May 12, 2021 Ruby


Table of contents


Ruby variable

A variable is a storage location that holds any data that can be used by any program.

Ruby supports five types of variables.

  • General lowercase letter, underscore beginning: Variable.
  • $ Start: Global variable.
  • @ Start: Instance variable.
  • @@ Beginning: Class variable class variables are shared throughout the inheritance chain
  • Capital letter beginning: Constant.

You've already learned about these variables in the previous sections, which will give you a detailed look at these five types of variables.

Ruby global variable

The global variable start$ with $. The value of the unitialized global variable is nil, which produces a warning when the -w option is used.

Assigning a value to a global variable changes the global state, so it is not recommended to use a global variable.

The following example shows the use of global variables.

#!/usr/bin/ruby

$global_variable = 10
class Class1
  def print_global
     puts "Global variable in Class1 is #$global_variable"
  end
end
class Class2
  def print_global
     puts "Global variable in Class2 is #$global_variable"
  end
end

class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global
Try it out . . .


Here, $global is a global variable. This results in the following:

Note: In Ruby, you can access the values of any variable or constant by placing the character of the . . . before a variable or constant.

Global variable in Class1 is 10
Global variable in Class2 is 10

Ruby instance variable

The instance variable begins with . The value of the instance variable that is not initialized is nil, which produces a warning when the -w option is used.

The following example shows the usage of the instance variable.

#!/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

# 创建对象
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# 调用方法
cust1.display_details()
cust2.display_details()
Try it out . . .


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

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 variables start with , and must be initialized before they can be used in method definitions.

Referencing an un initialized class variable produces an error. A class variable can be shared in a sub-class or sub-module of a class or module that defines it.

Overloading class variables produces a warning when the -w option is used.

The following example shows the use of class variables.

#!/usr/bin/ruby

class Customer
   @@no_of_customers=0
   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
    def total_no_of_customers()
       @@no_of_customers += 1
       puts "Total number of customers: #@@no_of_customers"
    end
end

# 创建对象
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# 调用方法
cust1.total_no_of_customers()
cust2.total_no_of_customers()
Try it out . . .


Here, @@no_of_customers is a class variable. This results in the following:

Total number of customers: 1
Total number of customers: 2

Ruby local variable

Local variables begin with lowercase letters or underscores. The scope of a local variable is from class, module, def, or do to the corresponding end or from the opening brace to the closing brace.

When an uninitialized local variable is called, it is interpreted as calling a method without parameters.

Assignments to unitialized local variables can also be declared as variables. T he variable will remain in existence until the end of the current domain. The life cycle of a local variable is determined when ruby parses.

In the example above, the local variables are id, name, and addr.

Ruby constant

The constant begins with a capital letter. Constants defined within a class or module can be accessed from within a class or module, and constants defined outside a class or module can be accessed globally.

Constants cannot be defined within a method. R eferencing an un initialized constant produces an error. Warnings are issued for constant assignments that have already been initialized.

#!/usr/bin/ruby

class Example
   VAR1 = 100
   VAR2 = 200
   def show
       puts "Value of first Constant is #{VAR1}"
       puts "Value of second Constant is #{VAR2}"
   end
end

# 创建对象
object=Example.new()
object.show
Try it out . . .


Here, VAR1 and VAR2 are constants. This results in the following:

Value of first Constant is 100
Value of second Constant is 200

Ruby pseudovaris

They are special variables that have the appearance of local variables but behave like constants. You cannot assign any values to these variables.

  • Self: The sink object of the current method.
  • true: The value that represents true.
  • false: The value that represents false.
  • nil: Represents the value of the undefined.
  • __FILE__: The name of the current source file.
  • __LINE__: The number of the current line in the source file.