Simple question concerning overloading

Discussion in 'Ruby' started by Mathias Weyel, Nov 13, 2004.

  1. 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
    Mathias Weyel, Nov 13, 2004
    #1
    1. Advertising

  2. 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
    gabriele renzi, Nov 13, 2004
    #2
    1. Advertising

  3. 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.
    Florian Gross, Nov 13, 2004
    #3
  4. 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.
    Florian Gross, Nov 13, 2004
    #4
  5. Robert Klemme, Nov 14, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,397
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    442
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    423
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    571
    John J. Lee
    Sep 30, 2005
  5. Carter
    Replies:
    5
    Views:
    274
    Carter
    Apr 29, 2008
Loading...

Share This Page