[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Simple question concerning overloading

Mathias Weyel

11/13/2004 1:55:00 PM

Hello!

I am quite new to Ruby and come directly from the world of Java. While
the lack of a static type system has proven to be somehow irritating to
me, I have stepped over a rather small problem concerning overloading of
methods and parameter names. In Java, this problem cannot occur due to
the static type system.

Here's what I want to do:

I have a class that serves as a container for objects. I want to have a
method "remove" that removes Objects from the list. This method should
exist twice. One which accepts an object and deletes the object that is
== to the given object and another one that accepts ints and deletes the
object at the given index. Here the solution in Java:

class Whatever {
List objects;
public void remove(Object o) {...}
public void remove(int i) {...}
}

The problem in converting this code to Ruby is that the remove method
has no type and, because of this, cannot be overloaded this way. While
having only one method and checking the type of the given argument
manually seems somehow wrong to me, I wonder, how to call the parameter.
The int parameter I would normally call "index" while the object
parameter would correspond to the objects stored in the list. What name
should the parameter get? I want to have a name that actually makes
sense, but "objectOrIndex" seems stupid.

Greetings

Mathias Weyel
5 Answers

gabriele renzi

11/13/2004 1:59:00 PM

0

Mathias Weyel ha scritto:

> Hello!
>
> I am quite new to Ruby and come directly from the world of Java. While
> the lack of a static type system has proven to be somehow irritating to
> me, I have stepped over a rather small problem concerning overloading of
> methods and parameter names. In Java, this problem cannot occur due to
> the static type system.

notice that there are modules (such as the poorly named StrongTyping or
types.rb) that allow you to attach type information to methods, even
allowing overloading. Also there is a module from Florian Gross (IIRC,
ans sorry for not typing the 'b' :) that allows you to use real MMD.

<snip>

> class Whatever {
> List objects;
> public void remove(Object o) {...}
> public void remove(int i) {...}
> }
>
> The problem in converting this code to Ruby is that the remove method
> has no type and, because of this, cannot be overloaded this way. While
> having only one method and checking the type of the given argument
> manually seems somehow wrong to me, I wonder, how to call the parameter.
> The int parameter I would normally call "index" while the object
> parameter would correspond to the objects stored in the list. What name
> should the parameter get? I want to have a name that actually makes
> sense, but "objectOrIndex" seems stupid.
>

why do you feel the need to have a single method?
I mean, they're actually doing differen things, so why not:
class Foo
def remove_object(obj)..end
def remove_index(idx)..end
end

HTH

Florian Gross

11/13/2004 2:19:00 PM

0

Mathias Weyel wrote:

> class Whatever {
> List objects;
> public void remove(Object o) {...}
> public void remove(int i) {...}
> }

This makes no sense in Ruby, because Numbers are also Objects. I'd
suggest naming the method .delete and .delete_at instead.

Its Me

11/13/2004 5:04:00 PM

0


"Florian Gross" <flgr@ccan.de> wrote

> This makes no sense in Ruby, because Numbers are also Objects. I'd
> suggest naming the method .delete and .delete_at instead.

Which brings me back to a question I've asked before. In 2.0 with keyword
arguments will

delete (obj)
and
delete(at: index)

be distinguishable methods? If not, I think we will not get the full benefit
of keyword arguments. I'd be forced to use delete_at where I would prefer to
use delete:at:

Also, will the keyword parameter names be accessible via reflection?


Florian Gross

11/13/2004 6:22:00 PM

0

itsme213 wrote:

> Which brings me back to a question I've asked before. In 2.0 with keyword
> arguments will
>
> delete (obj)
> and
> delete(at: index)
>
> be distinguishable methods? If not, I think we will not get the full benefit
> of keyword arguments. I'd be forced to use delete_at where I would prefer to
> use delete:at:

They will be from inside the method, I suppose. We can build real method
overloading around that easily.

Robert Klemme

11/14/2004 12:41:00 PM

0


"gabriele renzi" <rff_rff@remove-yahoo.it> schrieb im Newsbeitrag
news:Vqold.31698$Es2.683518@twister2.libero.it...


See also
http://www.rubygarden.org/ruby?MethodO...
http://www.rubygarden.org/ruby?R...

robert