From: Josh Cheek Date: 2011-11-27T17:08:22+09:00 Subject: Re: question about overloading --0015175dd82a7fda8004b2b2e3e9 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Nov 27, 2011 at 1:38 AM, Vitit Kantabutra wrote: > Back in 2007, somebody posted a question asking whether or not it is > possible to overload the constructor in Ruby. Someone else answered > that it is not possible to overload any method, not just the > constructor. Indeed, when I tried to overload the constructor in my own > code it did not work. > > However, in the code for fxruby's FXRegion class, the initialize method > is indeed overloaded, and it works! Why is that? Does it have > something to do with the Module? > > Here is the code and the Web site from whence it came: > > module Fox > class FXRegion > # Construct new empty region > def initialize; end > > # Construct new region copied from region _r_ (another FXRegion > instance). > def initialize(r); end > > # Construct new region from rectangle _rect_ (an FXRectangle > instance) > def initialize(rect); end > > # > # Construct rectangle region, where (_x_, _y_) are the coordinates > of the > # upper left-hand corner and (_w_, _h_) are its width and height. > # > def initialize(x, y, w, h); end > > # > # Construct polygon region from an array of points. Here, _points_ > is > # an array of FXPoint instances. > # > def initialize(points, winding=false); end > > > > http://www.koders.com/ruby/fid71E82B9184726294068BA353AB7E6C68DA98E25B.aspx?s=socket > > Another question: Why are there semicolons in the code? Is this really > Ruby? > > -- > Posted via http://www.ruby-forum.com/. > > My assumption would be that this code was auto-generated by some sort of code generator. You don't need the semicolons. You can override any method (there may be some restrictions in MRI, for performance reasons when dealing with certain objects like true/false/integers. With regards to the initialize method, you are expected to override it for the purpose of defining your own custom initialization. That's why it is there and that's why it gets invoked by the `new` method. If you want to override the `new` method, things will be more difficult, as you'll then have to take on the responsibility of things like allocating memory. --0015175dd82a7fda8004b2b2e3e9--