Introduction
Nowadays, the Ruby language has come into the limelight by various developers. It is a general-purpose and highly portable language used for developing static websites, applications, automation tools, and even data processing services.
But Ruby is an object-oriented programming language, so everything in the language is associated with an object. So if an error came to an object, how will Ruby find and solve these errors. There are many methods, one of them being tainting of objects. We will discuss all tainting of objects in Ruby while moving further with the blog, so without wasting any time further, let’s get on with our topic.
Objects in Ruby
As Ruby is an object-oriented language, so one way or another, everything is an object in Ruby. Ruby Supports everything like inheritance, encapsulation, polymorphism, operator overloading, etc. We can use the new keyword to create objects in Ruby. We can create as many objects as we want from a single class.
We can access any class attribute using an object created using the class name. They can contain any data like variables and methods. Each object points to the exact memory location for the variable and accessing method without allocating new memory.
Creating Objects in Ruby
We have to use a new keyword with the name of a class like XYZ to create objects in Ruby.
- We have created a class name XYZ. As you can see, we have to use the class keyword before the name, as we will use this name to create and define the object.
- We have commented inside the class for the reader to understand what is happening inside the class.
- Inside the class, we have used a predefined method, i.e., method initialize method.
- Now, we are creating an object of the class using new.
- The class will automatically initialize the method and the initialization of variables at the time of object creation. Here the object contains all the methods and variables of the XYZ class.
class XYZ
#Do the initialization here
def initialize(param1, param2)
# Here, we are initializing the variables.
@param1 = param1
@param2 = param2
end
end
B1 =XYZ.new(param1, param2)#Creation Of Object B1
B2 =XYZ.new(param1, param2)#Creation Of Object B2
B3 =XYZ.new(param1, param2)#Creation Of Object B3
B4 =XYZ.new(param1, param2)#Creation Of Object B4
Working of Objects in Ruby
In Ruby, the classes are the blueprint of Ruby, and every object at the time of creation has all the class properties.
- We have a class name XYZ that contains a constant and method.
- The new keyword will inform the class and ruby compiler that there is a request to create the new object.
- Now, the ruby compiler will associate all the properties and memory of the class to the object with the new keyword.
- Now, it depends on the user to create as many objects as he can create.
- Each object will have the same constant and method inside it.
Copying Objects
We can use two methods for copying the objects one is dup method, and the other is the clone method, but both of them will give a shallow copy of the object. If you are copying an object and that object contains a reference for some other objects, then only the reference of the referenced objects will be copied, not the whole referenced objects.
If the object to be cloned has an initialize copy function, dup and clone simply allocate a new, empty instance of the class and call the initialize copy method on it. The object to be copied is supplied as a parameter, and this copy function Object() can do whatever it wants with the copy. For instance, the initialize copy method could recursively copy an object's internal data. The result isn't just a shallow replica of the original.
Classes can directly produce any copy by overriding the dup and clone method.
The main difference between dup and clone method is that dup only copies the tainted state of the object, whereas the clone copies both the tainted and frozen state of the object. And the other difference between these two is that dup does not copy any singleton function, whereas clone does.
Marshaling Object
The class method Marshal.dump can save the state of the object if it is passed by this method. For an input/output stream, the Marshal.dump writes the object's state to the stream. Otherwise, it will return a binary string.
The Binding and Method objects are too dynamic to be marshaled.
Freezing Object
We can make any object frozen by calling its freezing method. The frozen object becomes immutable, and we can not change any of its internal states and cannot call any of its mutators.
If you copy a frozen object with the help of a clone, then the copied object will also remain frozen. But if you copy the frozen object with the help of dup, then the copy will not be frozen.