Introduction
For a developer working with classes is a common practice, like he has to implement various classes as he needs to modify various operations. But if he has to use the classes in multiple instances, does he have to create a class every time?
The answer is no. We can use objects as they will have the same variables and properties as the class. This will reduce the code complexity. But can we use objects anywhere in the whole code?
No, we cannot use the object in the whole code. We should know where the object is declared and where it will be accessible within the function. It will be determinable by the object’s lifetime. Don’t worry. We will learn about object lifetime while moving further in the blog, so let’s get on with our topic without wasting time.
Variables and Constants in Ruby
Variables are one of the most crucial features in ruby as they hold the data in the memory location used by any program. There are mainly five types of variables present in Ruby. We will learn all of them in this blog section.
Global Variables
The global variables in ruby begin with the ‘$’ symbol with a lower letter. Global variables can be initialized, and uninitialized global variables will have the value equal to nil and the procedure waring with the -w option.
It can be used within the block of initialization. It will have no scope after the completion of the function. Assigning global variables after the global status is not advisable as they will make the program cryptic. The example of the global variable is shown below:
#!/usr/bin/ruby
$global_variable = 10
class Class1
def print
puts "Global variable in Class1"
end
end
class Class2
def print
puts "Global variable in Class2"
end
end
class1obj = Class1.new
class2obj = Class2.new
class1obj.print_global
class2obj.print_global
Instance Variables
The instance variables in Ruby begin with @ symbol, as the name suggests. It belongs to one instance of the class.
Instance variables can be initialized, and uninitialized instance variables will have the value equal to nil and the procedure waring with the -w option. Any instance can access it within the method. Most of the time, we only have limited access to a particular class instance. They can be used without initialization.
#!/usr/bin/ruby
class Customer
def initialize(id, name, addr)
@cust_name = name
@cust_id = id
@cust_addr = addr
end
def display_details()
puts "Customer name #@cust_name"
puts "Customer id #@cust_id"
puts "Customer address #@cust_addr"
end
end
# Create Objects
cust1 = Customer.new("Wisdom Apartments", "John",1, Mumbai")
cust2 = Customer.new(" New Empire road"."Poul",2 , Delhi")
# Call Methods
cust1.display_details()
cust2.display_details()
Class Variables
They begin with @@ characters and need to be initialized before using in the methods. As the name suggests, they are class variables, so they belong to the whole class and can be used from anywhere in the class. Due to their availability in the whole class, if their value is changed at one instance, then their value will be changed for every other instance in the class.
The reference to an uninitialized class variable will throw an error. The class variables will be shared among all the descendants of the class. Overriding the class variables will issue -w warning.
#!/usr/bin/ruby
class Customer
@@no_of_customers = 0
def initialize(id, name, addr)
@cust_name = name
@cust_id = id
@cust_addr = addr
end
def display_details()
puts "Customer name #@cust_name"
puts "Customer id #@cust_id"
puts "Customer address #@cust_addr"
end
end
# Create Objects
cust1 = Customer.new("Wisdom Apartments", "John",1, Mumbai")
cust2 = Customer.new(" New Empire road"."Poul",2 , Delhi")
# Call Methods
cust1.display_details()
cust2.display_details()
Local Variables
They begin with an underscore (_) or lowercase letters. The local variables' scope varies from module to class and corresponding end. In short form, opening brace({) to closing brace(}).
When a user refers to an uninitialized local variable, the compiler refers to this call as a call to a method with no argument. We can use the assignment to the variables as declarations to the variables. The variables will be available until the scope of variables exists. The examples of local variables are id, addr, and name in the above example.
Pseudo variables
These unique variables are represented as local variables but behave like constants. As they are constant, you cannot assign any value to them.
- True: Value represents true.
- False: Value representing false.
- Self: The receiver object to the current object.
- _LINE_: The current line number in the source file.
- _FILE_: The name of the current source file.
- Nil: Value representing undefined.