Sunday, November 28, 2010

dup and clone: What's the difference?

dup and clone are used in ruby to create duplicate/clone of an object. So what's the difference between them? They differ in how they operate on frozen object. When frozen object is cloned using clone, the cloned object remains frozen, while when frozen object is duplicated using dup, the duplicated object is not frozen.

Saturday, November 27, 2010

Ruby: freezing your object

Before diving in, lets look into what are mutable and immutable objects.

Mutable object is object that can be modified anytime during its life time. Immutable object is object that cannot be modified after it is created.

In ruby object can be made immutable anytime using freeze method. Once the object is frozen, it cannot be modified further in its life time. Any attempt to modify frozen object results in TypeError. Let's take a look at simple example of freezing a string object.


Once str is frozen there is no way it can be unfrozen.

Ruby also provides frozen? method to check whether object is frozen or not.


One important point to take a note about freeze method here is that freeze makes object immutable and not object reference variable. Take a look at below code.


obj1 and obj3 points to one instance of FreezeTest and obj2 points to another instance of FreezeTest. Once obj1.freeze is invoked, instance of FreezeTest to which obj1 and obj3 are pointing is frozen and not obj1 and obj3 variables. Both these variables can be modified to point to any other instance as seen in above code.


So where can freeze method be useful? Two places I can think of

  1. Creating a true constant object: By true constant, I mean constant variable that refer to immutable object. Lets take example from rails. request.rb defines LOCALHOST constant as

    LOCALHOST = [/^127\.0\.0\.\d{1,3}$/, "::1", /^0:0:0:0:0:0:0:1(%.*)?$/].freeze

    By freezing array to which LOCALHOST points, rails makes sure that no one should be able to modify definition of localhost by removing and adding elements to array.

  2. Using object as hash key: Once object is used as hash key, we want to make sure that it's hash is not changed otherwise it will be difficult to find value associated with it. One of the way to achieve this is by freezing object and then using it as hash key.

What All Rubyist Should Know About Threads

Tuesday, November 23, 2010

Google App Engine, things to know

Goodbye Google App Engine (GAE) provides some details one should be aware before starting using GAE as platform for new killer web app.

Sunday, November 21, 2010

Using ActiveRecord without Rails

Following ruby code shows how to use active record in non rails environment.



Contents of database.yml: