Question about overloading

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®; 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?

On Sun, Nov 27, 2011 at 1:38 AM, Vitit Kantabutra
[email protected]wrote:

Here is the code and the Web site from whence it came:

Construct new region from rectangle rect (an FXRectangle

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.

On Nov 27, 2011, at 00:08 , Josh C. wrote:

You can override any method (there may be some restrictions in MRI, for
performance reasons when dealing with certain objects like
true/false/integers.

He’s asking about overloading, not overriding.

There is no way to overload a method in Ruby. That’s in part due to Ruby
being dynamically typed, and of “methods” being treated as “messages”.

Most of the time, you wouldn’t need to overload a method because of the
ducktyping nature of Ruby. Your method’s argument can exhibit the same
services no matter what their type is.
If you want varying parameters for a method, you can use the splat
operator
(*).

module Fox

def initialize(rect); end
is

an array of FXPoint instances.

def initialize(points, winding=false); end

In the example you provided, the methods are simply *overridden. *The
only
effective initialize method will be the last one.

Thank you for all your answers! The last one (posted by Bartosz
Dziewoński) is the correct one, but I so appreciate all responses. Now
I understand what’s going on.

FXRuby is not written in Ruby (it’s a C extension), and this is not
its real code.

What you see are just “stubs” used to generate the documentation (the
link you posted to leads to a file in /rdoc-sources/ directory - all
files in there are “fakes”). That’s why there are multiple
what-looks-like-overloads in there - the C initialize method behaves
differently when given different arguments, and this is a nice way to
display it. (Some Ruby core methods also have similar structure of
documentation, for example Array#slice:
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-slice)

FXRuby has some Ruby code, and it’s in the /lib/fox16/ directory. For
example:
http://www.koders.com/ruby/fid3A37B770F5119994AB0C79E275CC71ABDDB47E77.aspx

– Matma R.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs