Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / Ruby

Crazy Ctor Concept in Ruby: Part 2

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
1 Nov 2011LGPL32 min read 5.5K   3  
This is an extension to my old post about ctor concept in Ruby where I talked about how the Ctor concept has been implemented in Ruby and it’s quite different from other languages.

This is an extension to my old post about ctor concept in Ruby where I talked about how the Ctor concept has been implemented in Ruby and it’s quite different from other languages, at least from C#, because I know that language well.

Further to my findings on this, by reading other books and googling, I found out that actually Creation and Initialization happens at two different stages. At first I was confused about this, where it said Initialize is the first method called when you try to create a new object. Later findings of mine revealed that creation happens by calling the Class.new method and initialization happens by calling the Initialize method.

In Ruby, every time you create a new class, Ruby creates an object of type Class by calling the Class.new method. This new method further calls the allocate method to create a new object. This step is actually called Construction, and it is quite different. This operation happens behind the scenes, so from the developer perspective, there is no constructor in Ruby.

But the advantage here is that you can override the Class.new method in your type. By doing so, you get the advantage of making your type a wrapper class for some other type. Let me show you a sample code:

10530367_798c_625x625.jpg

As you can see from the above code, I have defined two classes and a Wrapper class which wraps the creation of my other classes. Now based on some conditions in this wrapper class, it creates and returns my other types.

One important thing to note in the above code is that I have used self.new() in the wrapper class. The new() method should be a class method, hence new is prefixed with the self keyword. If you do not use it, then new() will just be an instance method like other methods in your class, that means it will not override the Class.new() method, in other words, when you create Wrapper.new in your usage code, this instance new method will not be called at all.

If this had not been provided in Ruby, then I would have had to provide a method (perhaps class method) which would do the same, but this interface looks nice and Class.new is used by default every where to create objects of a type, and it is less confusing as well :)

Happy coding :)

License

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


Written By
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 -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
-- There are no messages in this forum --