Click here to Skip to main content
12,634,030 members (21,903 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as



Crazy Ctor concept in Ruby [TIP]

, 19 Oct 2011 LGPL3
Rate this:
Please Sign up or sign in to vote.
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).

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

def SomeMethod
puts @firstArg + @secondArg

instance =“hello”,” world”)

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”)

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.


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


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging ->

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 19 Oct 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid