Introduction
knowledge of objects is an essential requirement for a developer or a programmer dealing with Ruby language. The Ruby language works on objects. All of its components are related to objects either directly or indirectly.
Working with the object is a bit difficult task if you don’t know where the object is pointing or where it is stored. But don’t worry, we will learn all of them along with how to equate objects while moving further in this blog, so without wasting any more time, let's get on with our topic.
Object and its ID
Ruby is entirely dependent on objects for its work. Ruby can create objects that can perform various operations like inheritance, encapsulation, etc., methods and also consists of various properties.
Every data type is an object in Ruby like arrays, strings, boolean, hashes, integers, etc.
There are many public methods that an object contains which are responsible for its various properties, and with the help of these methods, we can know about the object. Here we will discuss one of the methods, i.e., Object_id.
Object IDs in Ruby
In Ruby, we have a method known as object_id for every object. As the name suggests, it gets the id for an object. It is unique for every object, so it acts as a distinguishable feature in distinguishing between various objects.
It refers to the address in the main memory where the object is stored. This will not change throughout the whole process when an object is present. Let’s understand this better with an example:
name1 = "Joey"
name2 = "Joey"
name1.object_id == name2.object_id # false
name2 = name1
name1.object_id == name2.object_id # true
As you can see in the above example, we have created two variables, both of string type, and initialized both with the same value.
In the next line, we compare the object_id of both to check whether they are the same or not, but we get false. Why?
As the data types of both objects are the same and their contents are also the same answer still comparing object_id of both of them gives false because each variable gets a different Location in the main memory to store their value when they are declared or initialized. So when we assign the same value to different objects, they don’t share the same memory location in the main memory, so the object_ids are different.
But in the second case, when we assign the value of name1 to variable name2 and then compare their object_ids, the answer is true. Why so?
This time, name1 and name2 share the same memory address. This happens because when assigning name2 with name1, we are not creating any other variable. We are just assigning the same address to name2 as that of name1, so after this operation, they have the same address and hence the same object_ids.
Some more examples of object_ids in Ruby are as follows:
num = 22
num.object_id # 76
developer = {"Joey": [8,7,6], "Ross": [0,1,2]}
developer.object_id # 86494844770580
numbers = (85..100).to_a
numbers.object_id # 86547956985540
programmer = programmer.new("programmer1")
programmer.object_id # 897425619709960
Integers, boolean and nil variables will give the same object_id again and again.
Now we will learn how to use them in a proper code.
class Developer
@@colors = {}
def initialise(name, colors)
@name = name
@@colors[object_id] = colors.map(&:to_sym)
end
def colors_mapping
return [] unless @@colors[object_id]
@@colors[object_id].each_with_object({}) do |color, hash|
hash[color] = rand(11..1000)
end
end
end
u = Developer.new("Joey", ["Black", "Yellow", "Grey", "White"])
p u.colors_mapping
In the above example, we have a developer class that takes the array of colors and a name in the initialize function. We assume a case when the color array will only be accessible in the class and will have no scope outside the class. So we will use a class variable to store the colors.
We will initialize the class variable as an empty hash, and then we will use object_id as the key to store colors in the initialize function.
As the class variable will keep the reference to all the instances created within the class using object_id, so we will make sure that with the collected data, there is no garbage data collected.
Then the color_mapping variable will use the hash key function to find and store the answer.
The value returned by the object_id method is unique and constant for the object's lifetime.