Introduction
The objects are the basic need/ requirement for a programmer or developer who is working with ruby, as all the things are related to objects in one way or another. The objects here are capable of performing various operations like encapsulation, inheritance, etc.
But if we work with objects, we have to use them repeatedly in various functions and operations. Writing them individually every time will be a time-consuming process. So is there any better way to use objects multiple times without creating them every time?
The answer to the above question is yes; We can use objects without creating a new object by either passing them by reference or by value. We will learn about both of them deeply while moving further in the blog, so without wasting any time further, let's get on with our topic.
Variable References and Types of Passes
Here, we will learn how ruby manipulates objects and variables, and how objects are passed between various programs in ruby. You might wonder what is different in Ruby as we just create an object and assign it to a variable, and then we can use that variable to access the object and change its values.
In languages like Perl and C++, when we assign an object’s value to the variable, it makes a copy of an object, and all the changes made by that variable happen on the copied object but in languages like python and javascript, when we assign an object equal to a variable it links object and variable, so the changes are affected directly to our object.
Variables and References
We can understand the object as some data that stores some information or state. It can be complex like it can represent a database connection or just can store a boolean value like true or false. We can assign objects to variables like this:
>> greeting = ‘Coding Ninjas’
=> "Coding Ninjas"
In the above statements, the ruby tells the greeting to associate its value with a string object with the value ‘Coding Ninjas’.
All the objects in Ruby have their separate object id, which can be obtained by calling ‘#object_id’ on the object in the question. Even the literals line boolean, number, string, nil. All of them have their individual object id:
>> 5.object_id
=> 11
>> n
nil.object_id
=> 8
>> true.object_id
=> 20
>> "abc".object_id
=> 70101471581080
If we reassign the variable, it doesn't mutate the object referenced by that variable. But the variable is bound to a new object. The original object will not be disconnected from the object.
Mutability
Objects can be of two types, i.e., mutable and immutable. The mutable objects can be mutated, meaning their values can be changed, whereas immutable objects cannot be mutated, which means their values cannot be changed. They need to be reassigned again.
Immutable Objects
These are the objects that have to be reassigned when used by variables. In Ruby, boolean values and numbers are immutable. Let’s learn this with an example.
>> number = 5
=> 5
>> number
=> 5
>> number = 3 * number
=> 15
>> number
=> 15
One might question that in the above example doesn't 5 changes to 15, so how it is immutable.
No, as we have seen above, we are not reassigning the values. We are creating a new integer value of 15 and assigning it to a number. There is no method available by which we can mutate an immutable object. While reassigning the number, the variable starts to reference another object as it disconnects the variable from the original object.
Mutable Objects
Some objects like numbers and boolean immutable objects in ruby rest are mutable. They allow the changes to its object state. You can perform either complex or more straightforward operations to mutable operations.
You can change the value of part of an object by the setter method. The array method will look like this:
>> a = [1, 2, 3, 4, 5]
>> a[1] = 0 # calls setter method
>> a # => [1, 0, 3, 4, 5]
Now we will see an example of how to mutate array elements with the help of a variable:
>> a = %w(a b c)
=> ["a", "b", "c"]
>> a.object_id
=> 70227178647540
>> a[0] = '-' # calls `Array#[]=` setter method
=> "-"
>> a
=> ["-", "b", "c"]
>> a.object_id
=> 70227178647540
As you can see in the above example, we use array a and its reference. The original array is not changed as the object id is still the same.
Types of Passes
There are mainly two types of passes. We will have their brief introduction in this blog section as we will learn about them in detail in the further part of this blog.
- Pass by Reference: In pass by reference, the variable is directly passed into the function, which means any changes made to the variable inside the function will be visible to the user as the original variable will also be changed.
- Pass by Value: In pass by value, the function receives a copy of the original variable which means any change in the variable inside the function will not affect or be visible on the original variable as the changes are done on a copy.