CodeProject · Ruby

Crazy Ctor concept in Ruby [TIP]

Dear Reader,

In ruby as we know, the ctor like concept is actually called initializers. Hence there is no really a constructor in ruby (perhaps i have not found even after googling for hours so far).

So to initialize all your local class data in ruby, you have to do some thing like this:

class Myclass

def initialize(firstArgument, secondArgument)
@firstArg = firstArgument
@secondArg = secondArgument
end

def SomeMethod
puts @firstArg + @secondArg
end
end

instance = Myclass.new(“hello”,” world”)
instance.SomeMethod

As per the above code, initialize() method is the initializer for this class. So the ruby interpreter automatically calls this method (initializer) when you use new keyword over the class name as per above code. A point worth mentioning here is that, ruby does not support method overloading like other languages, same is true for ctor.

But be aware here that the initializer does not always call new to create an object here, it may some times calls allocate to create classes. What actually it means is, if an object is already in the database (created earlier or some thing) then the same object is just allocated memory and all its internal data will be null on other hand by saying new, it creates a fresh memory for the object (brand new instance) with all its internal data being set or defined.

Now as per the above code, i have not specified any access modifiers viz private, public, etc. for the method initialize in the above code. So as per the ruby doc, by default all methods are public for a class but not for this initialize method though. Even if you explicitly provide a modifier to this method as public, still ruby treats it as private method.

So we can see that, the ruby interpreter do actually finds initialize method on a type on which new is used even though this method is private. So it is suggested by ruby experts/geeks that to not rely on initializer method in case your overriding this method to do some custom initialization mechanism.

At first, i got a bit curious that even specifying a modifier to this method still i was not able to call it explicitly using an instance. To find out if whats said every where is really true (you can call me crazy), i used Object.respond_to?() method to check if the method exists in this type i.e MyClass in above code. So i did some modifications to the above code:

if instance.respond_to?(“initialize”)
instance.initialize(“a”,”b”)
end

The if condition never gets passed, because respond_to? method can not find specified method in the MyClass instance. So i provided “include_private=true” as a second argument for this respond_to?() method. Then it was able to find the specified method, and thus the condition passed, but hey the call inside the if body do throw exception/error saying that private method is called.

Other way to prove this method is private is by using Module.private_methods() API on MyClass type. It shall lists what all the private methods MyClass has and up the root hierarchy. So as per its output, this initialize method gets listed. So thats also proves that ruby no matter what i do, still treats initialize method as private only.

Thanks

P.S: Your comments/votes are much appreciated.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s