Ruby variables


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


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

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.

Ruby constants

Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within the class or module, and constants defined outside the class or module can be accessed globally.

Constants cannot be defined within a method. Referencing an uninitialized constant produces an error. Assigning a value to an initialized constant will generate a warning.


#!/usr/bin/ruby # -*- coding: UTF-8 -*- class Example VAR1= 100< Span class="hl-code"> VAR2= 200< Span class="hl-code"> def show puts "The value of the first constant is #{VAR1}" puts "The value of the second constant is #{VAR2}" end end # Create an object< Span class="hl-code"> object=Example< Span class="hl-code">.new() object.show< /div>

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.