ruby – variables and constant in ruby Ruby Constants Ruby Pseudo-Variables

Ruby provides five types of variables:

  • Local Variables: variables defined in a method. Local variables are not available outside the method. You will see more details about method in subsequent chapter. Local variables begin with a lowercase letter or _.
  • Instance Variables: variables available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign @ followed by the variable name.
  • Class Variables: variables available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name.
  • Global Variables: Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign $.

1. Local Variables:

Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block’s opening brace to its close brace {}.

When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments.

Assignment to uninitialized local variables also serves as variable declaration. The variables start to exist until the end of the current scope is reached. The lifetime of local variables is determined when Ruby parses the program.

2. Instance Variables

Instance variables begin with @. Uninitialized instance variables have the value nil

Here is an example showing usage of Instance Variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class 

def initialize(id)
@cust_id=id
end

def display_details()
puts "Customer id #@cust_id"
end
end


cust1=Customer.new("1")
cust2=Customer.new("2")

# Call Methods
cust1.display_details()
cust2.display_details()

Output:

1
2
Customer id 1
Customer id 2

3. Class Variables

Class variables begin 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 descendants of the class or module in which the class variables are defined.

Here is an example showing usage of class variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class 
@@no_of_customers=0
def initialize(id)
@cust_id=id
end
def display_details()
puts "Customer id #@cust_id"
end
def total_no_of_customers()
@@no_of_customers += 1
puts "Total number of customers: #@@no_of_customers"
end
end


cust1=Customer.new("1")
cust2=Customer.new("2")

# Call Methods
cust1.total_no_of_customers()
cust2.total_no_of_customers()

Output:

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

4. Global Variables

Global variables begin with $. Uninitialized global variables have the value nil

Assignment to global variables alters global status. It is not recommended to use global variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$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

Output:

1
2
Global variable in Class1 is 10
Global variable in Class2 is 10

Ruby Constants

Constants begin with an uppercase letter.

Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.

Constants may not be defined within methods. Referencing an uninitialized constant produces an error. Making an assignment to a constant that is already initialized produces a warning.

1
2
3
4
5
6
7
8
9
10
11
12
13
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

Output:

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

Ruby Pseudo-Variables

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

  • self: The receiver object of the current method.
  • true: Value representing true.
  • false: Value representing false.
  • nil: Value representing undefined.
  • __FILE__: The name of the current source file.
  • __LINE__: The current line number in the source file.