newbie question: function overloading

Discussion in 'Ruby' started by Dimitrios Galanakis, Oct 2, 2003.

  1. I need to define a method that performs differently when operated on objects
    of different type (overloading). Currently I use various if's to check for
    the type of the object as follows:

    def somefunction(a)

    if a.kind_of?(someType)
    expression1
    return
    end

    if a.kind_of(someOtherType)
    expression2
    return

    end


    i am wondering if there is a simpler way to implement that without the if's,
    in a way similar to C++ overloading. I mean I would like to define two
    functions with the same name that will somehow be aware of the type of
    their argument...

    sincerely,

    DG
     
    Dimitrios Galanakis, Oct 2, 2003
    #1
    1. Advertising

  2. "Dimitrios Galanakis" <> schrieb im Newsbeitrag
    news:4i_eb.147$...
    > I need to define a method that performs differently when operated on

    objects
    > of different type (overloading). Currently I use various if's to check

    for
    > the type of the object as follows:
    >
    > def somefunction(a)
    >
    > if a.kind_of?(someType)
    > expression1
    > return
    > end
    >
    > if a.kind_of(someOtherType)
    > expression2
    > return
    >
    > end
    >
    >
    > i am wondering if there is a simpler way to implement that without the

    if's,
    > in a way similar to C++ overloading. I mean I would like to define two
    > functions with the same name that will somehow be aware of the type of
    > their argument...


    You can find further info here:
    http://www.rubygarden.org/ruby?MethodOverloading

    This might be of interest, too:
    http://www.rubygarden.org/ruby?KeywordArguments

    Regards

    robert
     
    Robert Klemme, Oct 6, 2003
    #2
    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,428
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    454
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    437
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    585
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    410
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page