Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
There are many times when a programmer spends a lot of time and effort getting the object properties and variables just right. It will be unfortunate if someone accidentally or intentionally changes the object's properties.
Perhaps an opaque object can be transmitted between two classes via a third-party object, ensuring that it arrives unchanged. Maybe you want to use an object as a hash key and ensure no one changes it while it's in use. Maybe something is corrupting one of your objects, and you want Ruby to throw an exception as soon as it happens.
Ruby provides a straightforward method to help with this. Any object in Ruby can be frozen by invoking Object#freeze.
How to Freeze Objects in Ruby?
Any object in Ruby can be frozen using the Object#freeze method. We can't change the instance variables of a frozen object, we can't connect singleton methods with it, and we can't add, delete, or edit the methods of a frozen class or module.
We may use the Object#frozen function to see if an object is frozen. If the object is frozen, it returns true; otherwise, it returns false. We may use the freeze method in an object to transform an object into a constant.
It's worth noting that a frozen object can't be unfrozen. The syntax for freezing and object is: -
Object_Name.freeze.
Example
Code
#class is defined
class Addition
# intializing values
def set_values(x, y)
@a, @b = x, y
end
# functions to access the variables
def get_A
@a
end
def get_B
@b
end
# functions to set the variables
def set_A=(value)
@a = value
end
def set_B=(value)
@b = value
end
end
# new object created
sum = Addition.new(10, 20)
# the below line of code freezes the object
sum.freeze
if( sum.frozen? )
puts "Object is Frozen."
else
puts "Object is Not Frozen."
end
# using setter functions
sum.set_A = 30
sum.set_B = 50
# using accessor functions
sum.get_A()
sum.get_B()
puts "A is: #{sum.get_A()}"
puts "B is: #{sum.get_B()}"
You can also try this code with Online Ruby Compiler
We used the sum.freeze method, due to which the object is now frozen, and we can't change the value of its method. Here, sum.frozen? The method is used to show whether the object is frozen or not.
Limitations
Variables referencing frozen objects can be updated, which is important to remember. This is due to the fact that only the objects themselves are frozen, not the variables that refer to them.
The following example demonstrates how a frozen item may be replaced with a fresh object that shares the same variable:
str = "Hi"
str.freeze
str = "New_Hi"
puts str
# the program prints the variable without showing any errors
You can also try this code with Online Ruby Compiler
The default root of all Ruby objects is an object. An object is a descendant of BasicObject, allowing for the creation of alternative object hierarchies. Object methods are available to all classes unless they are expressly overridden.
The Kernel module contains object mixes that make the built-in kernel functions globally available. You don't need to use the complete namespace when addressing constants in classes that inherit from the object. Referencing File inside YourClass, for example, will return the top-level File class.
The argument symbol in the descriptions of the object's methods refers to a symbol, which can be either a quoted-string or a Symbol.
How to freeze objects in Ruby?
The freeze method ensures that an object cannot be modified in Ruby. This method creates immutable objects such that any attempt to modify an object called the freeze method will result in the program throwing a runtime error.
How is a frozen object different from an ordinary object?
Unlike a normal object of a class, we cannot change the instance variables of a frozen object, we can't connect singleton methods with it, and we can't add, delete, or edit the methods of a frozen class or module.
What happens when you copy a frozen object?
This depends on the method that is being used. For example, if you call an object's clone method, the entire object state (whether it is frozen) is copied to the new object. On the other hand, dup typically copies only the object's contents where the new copy will not inherit the frozen status.
What are the limitations of the freeze method?
Variables referencing frozen objects can be updated, which is an important point to remember. This is due to the fact that only the objects themselves are frozen, not the variables that refer to them. Therefore the same variable name can be used again to refer to some other value.
Conclusion
In this article, we have extensively discussed Conditional Random Fields, their implementation, and their importance.
After reading about the Architecture of IoT, are you not feeling excited to read/explore more articles on the topic of Programming in IoT and learning Ruby? Don't worry; Coding Ninjas has you covered. To learn, see more blogs on Ruby, Ruby Documentation, and Programming Language for IoT. Check out this problem - Subarray Sum Divisible By K