Life without Method Overloading?

Discussion in 'Ruby' started by Derek Cannon, Apr 19, 2010.

  1. Derek Cannon

    Derek Cannon Guest

    How do Ruby programmers handle method overloading? In Java, I could
    easily create several methods of the same name that accept a variety of
    input.

    I know in Ruby, using *args you can accept an unlimited number of
    parameters. Do I just this with a series of if statements?

    Or is there a common way Ruby programmers handle this?

    Thanks again,
    Derek
    --
    Posted via http://www.ruby-forum.com/.
    Derek Cannon, Apr 19, 2010
    #1
    1. Advertising

  2. Hi,


    > Or is there a common way Ruby programmers handle this?


    There are some common patterns, like what you find in Rails for example:

    http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002313&name=find

    I think overloading is very much attached to static typing and not as
    useful in dynamic languages as it seems. Creating more methods is free
    and generally more expressive. Polymorphism is implicit (look up what we
    call 'duck-typing' here). I find that I rarely have the need for
    overloading in Ruby (or that I overload all the time, depending on which
    way you look at it - no type signatures means that you can pass in
    anything at all) - and when I need it, I use the rails-kind named
    arguments.

    greetings,
    kaspar
    Kaspar Schiess, Apr 19, 2010
    #2
    1. Advertising

  3. On Sun, Apr 18, 2010 at 10:53 PM, Derek Cannon
    <> wrote:
    > How do Ruby programmers handle method overloading? In Java, I could
    > easily create several methods of the same name that accept a variety of
    > input.
    >
    > I know in Ruby, using *args you can accept an unlimited number of
    > parameters. Do I just this with a series of if statements?


    Mostly, I think instead of creating several methods of the same name, Ruby
    programmers tend to create methods with different names. Some of the use
    cases of overloading can also be addressed with optional arguments, trailing
    hash arguments, and so on.
    Christopher Dicely, Apr 19, 2010
    #3
  4. Derek Cannon

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Apr 19, 2010 at 12:53 AM, Derek Cannon
    <>wrote:

    > How do Ruby programmers handle method overloading? In Java, I could
    > easily create several methods of the same name that accept a variety of
    > input.
    >
    > I know in Ruby, using *args you can accept an unlimited number of
    > parameters. Do I just this with a series of if statements?
    >
    > Or is there a common way Ruby programmers handle this?
    >
    > Thanks again,
    > Derek
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    An easy thing to do is just normalize the data.

    def join_strings(a,b)
    a.to_s + b.to_s
    end

    join_strings 'a' , 'b' # => "ab"
    join_strings 1 , 2 # => "12"
    Josh Cheek, Apr 19, 2010
    #4
  5. Derek Cannon

    botp Guest

    On Mon, Apr 19, 2010 at 1:53 PM, Derek Cannon
    <> wrote:
    > How do Ruby programmers handle method overloading?


    ruby programmers do not need it

    > In Java, I could easily create several methods of the same name that accept a variety of
    > input.
    >


    maybe java people need it :)

    > I know in Ruby, using *args you can accept an unlimited number of
    > parameters. Do I just this with a series of if statements?


    no. you're doing it like in static languages.

    > Or is there a common way Ruby programmers handle this?


    it comes natural when you are *object*-oriented. there will be no need
    for overload. you wont even think about it.

    maybe you could show us a sample use case of method overloading since
    i cannot think of one right now ;-)

    best regards -botp
    botp, Apr 19, 2010
    #5
  6. Derek Cannon

    Derek Cannon Guest

    > maybe you could show us a sample use case of method overloading since
    > i cannot think of one right now ;-)


    Sure. A simple example of what I wanted to do (using method overloading
    in this example for lack of better know-how):

    Class XYZ

    def initialize(title, days, time, professor)
    @title = title
    @days = days
    @time = time
    @professor = professor
    end

    def initialize(title, days, time, professor, lab_time, lab_days)
    @title = title
    @days = days
    @time = time
    @professor = professor
    @lab_time = lab_time
    @lab_days = lab_days
    end

    end
    --
    Posted via http://www.ruby-forum.com/.
    Derek Cannon, Apr 19, 2010
    #6
  7. On Mon, Apr 19, 2010 at 10:39 AM, Derek Cannon
    <> wrote:
    >> maybe you could show us a sample use case of method overloading since
    >> i cannot think of one right now ;-)

    >
    > Sure. A simple example of what I wanted to do (using method overloading
    > in this example for lack of better know-how):
    >


    This one is easy:

    Class XYZ
    def initialize(title, days, time, professor, lab_time=3Dnil, lab_days=3Dn=
    il)
    @title =3D title
    =A0@days =3D days
    =A0@time =3D time
    =A0@professor =3D professor
    =A0@lab_time =3D lab_time
    =A0@lab_days =3D lab_days
    end
    end

    Jesus.
    Jesús Gabriel y Galán, Apr 19, 2010
    #7
    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,395
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    440
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    422
    Steve Holden
    Sep 30, 2005
  4. Alex Gusarov
    Replies:
    5
    Views:
    516
    Alex Gusarov
    May 28, 2008
  5. Matt Gregory
    Replies:
    6
    Views:
    145
    Öö Tiib
    Jun 13, 2014
Loading...

Share This Page