DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Freezing Objects

04.23.2010
| 3979 views |
  • submit to reddit
        Source: <a href="http://rubylearning.com/satishtalim/mutable_and_immutable_objects.html">Mutable and Immutable Objects: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</a> [rubylearning.com]

<snip>
The freeze method in class Object prevents you from changing an object, effectively turning an object into a constant. After we freeze an object, an attempt to modify it results in TypeError. The following program p050xfreeze.rb illustrates this:

str = 'A simple string. '  
str.freeze  
begin  
  str << 'An attempt to modify.'  
rescue => err  
  puts "#{err.class} #{err}"  
end  
# The output is - TypeError can't modify frozen string

However, freeze operates on an object reference, not on a variable. This means that any operation resulting in a new object will work. This is illustrated by the following example:

str = 'Original string - '  
str.freeze  
str += 'attachment'  
puts str  
# Output is - Original string - attachment

The expression str + 'attachment' is evaluated to a new object, which is then assigned to str. The object is not changed, but the variable str now refers to a new object.
frozen?

A method frozen? tells you whether an object is frozen or not. Let's take an example here:

a = b = 'Original String'  
b.freeze  
puts a.frozen? # true  
puts b.frozen? # true  
a = 'New String'  
puts a  
puts b  
puts a.frozen? # false  
puts b.frozen? # true
      

Let us understand what we are doing here - a and b are two variables both of which are pointing to a string object - Original String. We then freeze the object Original String. Hence both a and b are now pointing to the frozen object Original String. This is verified by the statements puts a.frozen? and puts b.frozen?. Next, we create a new string object New String and make variable a point to this new object New String. Variable b is still pointing to the frozen object while a is not. This verified by the last 2 statements of the program.

Usage:
Ruby sometimes copies objects and freezes the copies. When you use a string as a hash key, Ruby actually copies the string, freezes the copy, and uses the copy as the hash key: that way, if the original string changes later on, the hash key isn't affected.

Ruby's internal file operations work from a frozen copy of a filename instead of using the filename directly. If another thread modifies the original filename in the middle of an operation that's supposed to be atomic, there's no problem: Ruby wasn't relying on the original filename anyway. You can adopt this copy-and-freeze pattern in multi-threaded code to prevent a data structure you're working on from being changed by another thread.

Another common programmer-level use of this feature is to freeze a class in order to prevent future modifications to it.

Note: Whenever an object in Ruby has no reference to it, then that object is marked for removal and the garbage collector will remove that object based on its algorithm. There is no way to access an un-referenced object.
</snip>