home PYTHONJAVA
 

Ruby variables

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 letters, beginning with an underscore: variable (Variable).
  • $ at the beginning: Global variable.
  • @ at the beginning: Instance variable.
  • @@At the beginning: Class variable class variables are shared throughout the inheritance chain
  • Capital letter begins with: Constant.

You've already seen these variables in the previous chapters, and this chapter will walk you through the five types of variables in detail.

Ruby global variables

Global variables start with $. The value of an uninitialized global variable is nil, which is warned when the -w option is used.

Assigning a global variable changes the global state, so global variables are not recommended.

The following example shows the usage of global variables.

Instance

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

Instance

#!/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 variables begin with @@ and must be initialized before they can be used in method definitions.

Referencing an uninitialized class variable will generate an error. A class variable can be shared and used in a subclass or submodule of a class or module that defines it.

Overloading a class variable generates a warning when the -w option is used.

The following example shows the usage of class variables.

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

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

Ruby local variables

Local variables begin with a lowercase letter or an underscore _. The scope of a local variable is from class, module, def, or do to the corresponding end or from the left brace to the right brace {}.

When an uninitialized local variable is called, it is interpreted as calling a method with no arguments.

Assigning an uninitialized local variable can also be considered as a variable declaration. The variable will persist until the end of the current field. The life cycle of a local variable is determined by the Ruby parser.

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

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

The value of the first constant 100
The value of the second constant 200

Ruby pseudo-variables

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 for the current method.
  • true: A value that represents true.
  • false: represents the value of false.
  • nil: represents the value of undefined.
  • __FILE__: The name of the current source file.
  • __LINE__: The number of the current line in the source file.





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