How can I overload a method in Ruby

Discussion in 'Ruby' started by Zhao Yi, Nov 17, 2008.

  1. Zhao Yi

    Zhao Yi Guest

    This is my class definition:
    class Impl
    include MyModule
    def fun()
    puts ("fun")
    end

    def fun(directory)
    puts ("fun directory")
    end
    end
    m=Impl.new()
    m.fun()

    When I run this program, I got this error:

    class_objects/module.rb:22:in `fun': wrong number of arguments (0 for 1)
    (ArgumentError)
    from class_objects/module.rb:22

    I declared two methods with the same name but different parameters. Why
    can't ruby support overload method?
    --
    Posted via http://www.ruby-forum.com/.
    Zhao Yi, Nov 17, 2008
    #1
    1. Advertising

  2. Zhao Yi

    Ryan Davis Guest

    On Nov 16, 2008, at 23:26 , Zhao Yi wrote:

    short answer: you can't (via normal ruby).

    > When I run this program, I got this error:
    >
    > class_objects/module.rb:22:in `fun': wrong number of arguments (0
    > for 1)
    > (ArgumentError)
    > from class_objects/module.rb:22
    >
    > I declared two methods with the same name but different parameters.
    > Why
    > can't ruby support overload method?


    because it wasn't designed to, that's why.

    You can use default args to do what you want above:

    > def fun dir = nil
    > puts "fun #{dir}"
    > end
    Ryan Davis, Nov 17, 2008
    #2
    1. Advertising

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

    there is no overloading, only overriding.


    You can have variable number of arguments by writing

    def fun(*params)
    #here params is an array of your arguments, [] if none.
    end

    you can also have a default value for your parameter:

    class Impl
    include MyModule

    def fun(directory = "")
    puts "fun #{directory}"
    end

    end
    m=Impl.new()
    m.fun()
    # >> fun
    m.fun "/dev/null"
    # >> fun /dev/null"

    On 17.11.2008, at 07:26 , Zhao Yi wrote:

    > This is my class definition:
    > class Impl
    > include MyModule
    > def fun()
    > puts ("fun")
    > end
    >
    > def fun(directory)
    > puts ("fun directory")
    > end
    > end
    > m=Impl.new()
    > m.fun()
    >
    > When I run this program, I got this error:
    >
    > class_objects/module.rb:22:in `fun': wrong number of arguments (0
    > for 1)
    > (ArgumentError)
    > from class_objects/module.rb:22
    >
    > I declared two methods with the same name but different parameters.
    > Why
    > can't ruby support overload method?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Einar Magnús Boson, Nov 17, 2008
    #3
  4. Zhao Yi

    Zhao Yi Guest

    Ryan Davis wrote:
    > On Nov 16, 2008, at 23:26 , Zhao Yi wrote:
    >
    > short answer: you can't (via normal ruby).
    >
    >> When I run this program, I got this error:
    >>
    >> class_objects/module.rb:22:in `fun': wrong number of arguments (0
    >> for 1)
    >> (ArgumentError)
    >> from class_objects/module.rb:22
    >>
    >> I declared two methods with the same name but different parameters.
    >> Why
    >> can't ruby support overload method?

    >
    > because it wasn't designed to, that's why.
    >
    > You can use default args to do what you want above:


    I am new to Ruby. What's do you mean by default args?
    thanks
    --
    Posted via http://www.ruby-forum.com/.
    Zhao Yi, Nov 17, 2008
    #4
  5. Zhao Yi

    Zhao Yi Guest

    Einar Magnús Boson wrote:
    > there is no overloading, only overriding.
    >
    >
    > You can have variable number of arguments by writing
    >
    > def fun(*params)
    > #here params is an array of your arguments, [] if none.
    > end
    >
    > you can also have a default value for your parameter:
    >
    > class Impl
    > include MyModule
    >
    > def fun(directory = "")
    > puts "fun #{directory}"
    > end
    >
    > end
    > m=Impl.new()
    > m.fun()
    > # >> fun
    > m.fun "/dev/null"
    > # >> fun /dev/null"


    ok I understand. Just curious, overload is a good feature in OO
    programming. Why doesn't Ruby support it? If I use the method you
    mentioned, I have to check the parameters and use if - else or swith to
    distinguish them.
    --
    Posted via http://www.ruby-forum.com/.
    Zhao Yi, Nov 17, 2008
    #5
  6. RnJvbTogWmhhbyBZaSBbbWFpbHRvOnlvdWhhb2RleWlAZ21haWwuY29tXSANCiMgb2sgSSB1bmRl
    cnN0YW5kLiBKdXN0IGN1cmlvdXMsIG92ZXJsb2FkIGlzIGEgZ29vZCBmZWF0dXJlIGluIE9PIA0K
    IyBwcm9ncmFtbWluZy4gV2h5IGRvZXNuJ3QgUnVieSBzdXBwb3J0IGl0PyBJZiBJIHVzZSB0aGUg
    bWV0aG9kIHlvdSANCiMgbWVudGlvbmVkLCBJIGhhdmUgdG8gY2hlY2sgdGhlIHBhcmFtZXRlcnMg
    YW5kIHVzZSBpZiAtIGVsc2UgDQojIG9yIHN3aXRoIHRvIGRpc3Rpbmd1aXNoIHRoZW0uDQoNCm1h
    eWJlIGJlY2F1c2Ugb2YgdGhlIGZmIGRpbGVtbWE6DQoNCmRlZiBtKDEpDQplbmQNCg0KZGVmIG0o
    Im9uZSIpDQplbmQNCg0KDQpydWJ5IHdpbGwgYmUgZm9yY2VkIHRvIGNoZWNrIHR5cGUuLi4gd2Mg
    bGVhZHMgdG8gc3RhdGljIHR5cGluZy4uLiB3YyBpcyBub3QgbWF0eiBydWJ5Li4NCg0KYnR3LCBa
    aGFvLCBjYW4geW91IGdpdmUgdXMgYSByZWFsIGNhc2Ugd2hlcmUgeW91IHJlYWxseSBuZWVkIHRv
    IGRvIG1ldGhvZCBsb2FkaW5nPw0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
    Peña, Botp, Nov 17, 2008
    #6
  7. I find that overriding is much more powerful. You can override just =20
    about any method.
    Try

    class Fixnum
    def to_s
    "this is kinda stupid"
    end
    end

    puts 5

    Overloading is mostly used for default arguments anyways isn't it? and =20=

    also requires static typing I think.



    Einar Magn=FAs Boson
    +354-661 1649



    On 17.11.2008, at 07:50 , Zhao Yi wrote:

    > Einar Magn=FAs Boson wrote:
    >> there is no overloading, only overriding.
    >>
    >>
    >> You can have variable number of arguments by writing
    >>
    >> def fun(*params)
    >> #here params is an array of your arguments, [] if none.
    >> end
    >>
    >> you can also have a default value for your parameter:
    >>
    >> class Impl
    >> include MyModule
    >>
    >> def fun(directory =3D "")
    >> puts "fun #{directory}"
    >> end
    >>
    >> end
    >> m=3DImpl.new()
    >> m.fun()
    >> # >> fun
    >> m.fun "/dev/null"
    >> # >> fun /dev/null"

    >
    > ok I understand. Just curious, overload is a good feature in OO
    > programming. Why doesn't Ruby support it? If I use the method you
    > mentioned, I have to check the parameters and use if - else or swith =20=


    > to
    > distinguish them.
    > --=20
    > Posted via http://www.ruby-forum.com/.
    >
    Einar Magnús Boson, Nov 17, 2008
    #7
  8. Zhao Yi

    Zhao Yi Guest

    Peña, Botp wrote:
    > From: Zhao Yi [mailto:]
    > # ok I understand. Just curious, overload is a good feature in OO
    > # programming. Why doesn't Ruby support it? If I use the method you
    > # mentioned, I have to check the parameters and use if - else
    > # or swith to distinguish them.
    >
    > maybe because of the ff dilemma:
    >
    > def m(1)
    > end
    >
    > def m("one")
    > end
    >
    >
    > ruby will be forced to check type... wc leads to static typing... wc is
    > not matz ruby..
    >
    > btw, Zhao, can you give us a real case where you really need to do
    > method loading?
    >
    > kind regards -botp


    I want to write a build method which will build source code in a
    directory. I assume there are two build methods, one doesn't have
    parameter while the other have one parameter. The method without
    parameter will build the source code in default directory, the method
    with one parameter will build the source code in the directory which is
    specified by this parameter. With method overloading, I can write them
    as below:

    def build()
    make
    end

    def build(directory)
    cd directory
    make
    end

    but without overloading, I have to do this:
    def build(directory)
    if(directory == nil)
    make
    else
    cd directory
    make
    endif
    end
    --
    Posted via http://www.ruby-forum.com/.
    Zhao Yi, Nov 17, 2008
    #8
  9. RnJvbTogWmhhbyBZaSBbbWFpbHRvOnlvdWhhb2RleWlAZ21haWwuY29tXSANCiMgZGVmIGJ1aWxk
    KCkNCiMgICAgIG1ha2UNCiMgZW5kDQojIA0KIyBkZWYgYnVpbGQoZGlyZWN0b3J5KQ0KIyAgICAg
    Y2QgZGlyZWN0b3J5DQojICAgICBtYWtlDQojIGVuZA0KIyANCiMgYnV0IHdpdGhvdXQgb3Zlcmxv
    YWRpbmcsIEkgaGF2ZSB0byBkbyB0aGlzOg0KIyBkZWYgYnVpbGQoZGlyZWN0b3J5KQ0KIyAgICAg
    aWYoZGlyZWN0b3J5ID09IG5pbCkNCiMgICAgICAgbWFrZQ0KIyAgICAgZWxzZQ0KIyAgICAgICBj
    ZCBkaXJlY3RvcnkNCiMgICAgICAgbWFrZQ0KIyAgICAgZW5kaWYNCiMgZW5kDQoNCnRyeSBpdCBs
    aWtlLA0KDQpkZWYgYnVpbGQgZGlyZWN0b3J5PW5pbA0KICBjZCBkaXJlY3RvcnkgdW5sZXNzIGRp
    cmVjdG9yeS5uaWw/DQogIG1ha2UNCmVuZA0K
    Peña, Botp, Nov 17, 2008
    #9
  10. RnJvbTogUGXDsWEsIEJvdHAgW21haWx0bzpib3RwQGRlbG1vbnRlLXBoaWwuY29tXSANCiMgZGVm
    IGJ1aWxkIGRpcmVjdG9yeT1uaWwNCiMgICBjZCBkaXJlY3RvcnkgdW5sZXNzIGRpcmVjdG9yeS5u
    aWw/DQojICAgbWFrZQ0KIyBlbmQNCg0KDQpvciBpZiB5b3UgZG8gbm90IGxpa2UgdGhlIGlmL3Vu
    bGVzcyBwYXJ0LA0KDQpkZWYgYnVpbGQgZGlyZWN0b3J5PSIuIg0KICBjZCBkaXJlY3RvcnkNCiAg
    bWFrZQ0KZW5kDQoNCg0KDQogDQo=
    Peña, Botp, Nov 17, 2008
    #10
  11. Zhao Yi

    Zhao Yi Guest

    I am a Java developer and new to Ruby. I think I really need some time
    to familiar with Ruby.

    Anyway, thanks all of your kindly reply.

    Yi
    --
    Posted via http://www.ruby-forum.com/.
    Zhao Yi, Nov 17, 2008
    #11
  12. Zhao Yi

    Trans Guest

    On Nov 17, 2:26=A0am, Zhao Yi <> wrote:
    > This is my class definition:
    > class Impl
    > =A0 include MyModule
    > =A0 def fun()
    > =A0 =A0 puts ("fun")
    > =A0 end
    >
    > =A0 def fun(directory)
    > =A0 =A0 puts ("fun directory")
    > =A0 end
    > end
    > m=3DImpl.new()
    > m.fun()
    >
    > When I run this program, I got this error:
    >
    > class_objects/module.rb:22:in `fun': wrong number of arguments (0 for 1)
    > (ArgumentError)
    > =A0 from class_objects/module.rb:22
    >
    > I declared two methods with the same name but different parameters. Why
    > can't ruby support overload method?


    Although it is not the "Ruby Way" so to speak, Ruby is flexible enough
    to allow for ways to do it. For instance:

    require 'facets/overload'

    class X
    def x
    "hello"
    end

    overload :x, Integer do |i|
    i
    end

    overload :x, String, String do |s1, s2|
    [s1, s2]
    end
    end
    Trans, Nov 17, 2008
    #12
  13. Zhao Yi <> writes:
    > I want to write a build method which will build source code in a
    > directory. I assume there are two build methods, one doesn't have
    > parameter while the other have one parameter. The method without
    > parameter will build the source code in default directory, the method
    > with one parameter will build the source code in the directory which is
    > specified by this parameter. With method overloading, I can write them
    > as below:
    >
    > def build()
    > make
    > end
    >
    > def build(directory)
    > cd directory
    > make
    > end


    Call it: buildWithDirectory(directory)

    > but without overloading, I have to do this:
    > def build(directory)
    > if(directory == nil)
    > make
    > else
    > cd directory
    > make
    > endif
    > end


    If you insist on one name for both, then:

    def build(directory=nil)
    ...
    end

    so you can call it as build() or build(dir).

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Nov 17, 2008
    #13
  14. Zhao Yi

    Brian Adkins Guest

    (Pascal J. Bourguignon) writes:

    > Zhao Yi <> writes:
    >> I want to write a build method which will build source code in a
    >> directory. I assume there are two build methods, one doesn't have
    >> parameter while the other have one parameter. The method without
    >> parameter will build the source code in default directory, the method
    >> with one parameter will build the source code in the directory which is
    >> specified by this parameter. With method overloading, I can write them
    >> as below:
    >>
    >> def build()
    >> make
    >> end
    >>
    >> def build(directory)
    >> cd directory
    >> make
    >> end

    >
    > Call it: buildWithDirectory(directory)


    build_with_directory would probably be a more conventional Ruby function name.

    --
    Brian Adkins
    http://www.lojic.com/
    http://lojic.com/blog/
    Brian Adkins, Nov 17, 2008
    #14
  15. Zhao Yi

    James Coglan Guest

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

    2008/11/17 Brian Adkins <>

    > (Pascal J. Bourguignon) writes:
    >
    > > Zhao Yi <> writes:
    > >> I want to write a build method which will build source code in a
    > >> directory. I assume there are two build methods, one doesn't have
    > >> parameter while the other have one parameter. The method without
    > >> parameter will build the source code in default directory, the method
    > >> with one parameter will build the source code in the directory which is
    > >> specified by this parameter. With method overloading, I can write them
    > >> as below:
    > >>
    > >> def build()
    > >> make
    > >> end
    > >>
    > >> def build(directory)
    > >> cd directory
    > >> make
    > >> end

    > >
    > > Call it: buildWithDirectory(directory)

    >
    > build_with_directory would probably be a more conventional Ruby function
    > name.



    In this case, this would be more Ruby-idiomatic:

    def build(directory = nil)
    cd directory if directory
    make
    end
    James Coglan, Nov 17, 2008
    #15
  16. Zhao Yi

    Joe Wölfel Guest

    It's interesting that without the feature of overloading the code is =20
    both shorter and more readable.


    On 17 nov. 08, at 03:05, Pe=F1a, Botp wrote:

    > From: Zhao Yi [mailto:]
    > # def build()
    > # make
    > # end
    > #
    > # def build(directory)
    > # cd directory
    > # make
    > # end
    > #
    > # but without overloading, I have to do this:
    > # def build(directory)
    > # if(directory =3D=3D nil)
    > # make
    > # else
    > # cd directory
    > # make
    > # endif
    > # end
    >
    > try it like,
    >
    > def build directory=3Dnil
    > cd directory unless directory.nil?
    > make
    > end
    Joe Wölfel, Nov 17, 2008
    #16
  17. Zhao Yi

    Brian Adkins Guest

    James Coglan <> writes:

    > [Note: parts of this message were removed to make it a legal post.]
    >
    > 2008/11/17 Brian Adkins <>
    >
    >> (Pascal J. Bourguignon) writes:
    >> > Call it: buildWithDirectory(directory)

    >>
    >> build_with_directory would probably be a more conventional Ruby function
    >> name.

    >
    >
    > In this case, this would be more Ruby-idiomatic:
    >
    > def build(directory = nil)
    > cd directory if directory
    > make
    > end


    Yes, Pascal already provided a version with default arg values in the
    parent.

    --
    Brian Adkins
    http://www.lojic.com/
    http://lojic.com/blog/
    Brian Adkins, Nov 17, 2008
    #17
  18. Zhao Yi

    Trans Guest

    On Nov 17, 2:22=A0pm, Joe W=F6lfel <> wrote:
    > It's interesting that without the feature of overloading the code is =A0
    > both shorter and more readable.


    Well, default arguments are a different matter really. That's all that
    is needed in this example case, fair enough. But in more complex cases
    overloading can be advantageous because it allows for code to be split
    up into smaller, more manageable chunks. But Ruby would probably need
    to support it internally for the feature to be especially beneficial.
    Hmm... I suppose that would require Ruby a bit more like Duby?

    http://blog.headius.com/2008/03/duby-type-inferred-ruby-like-jvm.html

    T.
    Trans, Nov 17, 2008
    #18
  19. Trans wrote:
    >
    > On Nov 17, 2:22 pm, Joe Wölfel <> wrote:
    >> It's interesting that without the feature of overloading the code is
    >> both shorter and more readable.

    >
    > Well, default arguments are a different matter really. That's all that
    > is needed in this example case, fair enough. But in more complex cases
    > overloading can be advantageous because it allows for code to be split
    > up into smaller, more manageable chunks. But Ruby would probably need
    > to support it internally for the feature to be especially beneficial.
    > Hmm... I suppose that would require Ruby a bit more like Duby?
    >
    > http://blog.headius.com/2008/03/duby-type-inferred-ruby-like-jvm.html
    >
    > T.
    >


    I know this will not work in every case, but can't you check the type of
    input in a function like foo(x) and convert it to the desired form or
    perform the required operation? I agree that this adds more code, and
    probably more places for errors, but it places all the code for foo in
    one place. I used this when making my own version of rational(). It
    would accept one or two integers, floats, strings, or rational numbers,
    or combination of them. I only had to change initialize without having
    to copy all the other code to methods like rational_f(), rational_i(), etc.
    Michael W. Ryder, Nov 17, 2008
    #19
    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. Piotre Ugrumov
    Replies:
    3
    Views:
    362
    Nick Hounsome
    Jan 25, 2004
  2. Allen
    Replies:
    2
    Views:
    624
    Rolf Magnus
    Aug 27, 2008
  3. ofranko
    Replies:
    3
    Views:
    347
    osmium
    Jan 7, 2009
  4. Simon Strandgaard

    overload method in module_eval, how?

    Simon Strandgaard, Nov 10, 2003, in forum: Ruby
    Replies:
    12
    Views:
    220
    Simon Strandgaard
    Nov 10, 2003
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    237
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page