YouTip LogoYouTip

Ruby Variable

Ruby Variables

Variables are storage locations that hold data which can be used by any program.

Ruby supports five types of variables.

  • Starts with a lowercase letter or underscore: Variable.
  • Starts with $: Global variable.
  • Starts with @: Instance variable.
  • Starts with @@: Class variable. Class variables are shared throughout the inheritance chain.
  • Starts with an uppercase letter: Constant.

You have already seen these variables in previous chapters. This chapter will explain these five types of variables in detail.

Global variables start with $. An uninitialized global variable has the value nil, and using the -w option will produce a warning.

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

The following example demonstrates the use of global variables.

Example

$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 yourself Β»

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

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

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

Instance variables start with @. An uninitialized instance variable has the value nil, and using the -w option will produce a warning.

The following example demonstrates the use of instance variables.

Example

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 yourself Β»

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

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

Class variables start with @@ and must be initialized before they can be used in method definitions.

Referencing an uninitialized class variable produces an error. Class variables are shared among subclasses or submodules of the class or module where they are defined.

Using the -w option will produce a warning when overriding a class variable.

The following example demonstrates the use of class variables.

Example

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 yourself Β»

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

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

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

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

Assigning a value to an uninitialized local variable also serves as a variable declaration. The variable exists until the current scope ends. The lifetime of a local variable is determined when Ruby parses the program.

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

Constants start with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside can be accessed globally.

Constants cannot be defined within methods. Referencing an uninitialized constant produces an error. Assigning a value to an already initialized constant produces a warning.

Example

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 yourself Β»

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

Value of first constant is 100
Value of second constant is 200

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

  • self: The receiver object of the current method.
  • true: Represents the value true.
  • false: Represents the value false.
  • nil: Represents the undefined value.
  • __FILE__: The name of the current source file.
  • __LINE__: The current line number in the source file.
← Ruby OperatorRuby Class Case Study β†’