Forum: Ruby How can I overload a method in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2008-11-17 08:29
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?
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2008-11-17 08:46
(Received via mailing list)
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:
87349a7a95b3f2e83c20194ef122885c?d=identicon&s=25 Einar Magnús Boson (Guest)
on 2008-11-17 08:50
(Received via mailing list)
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"
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2008-11-17 08:50
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
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2008-11-17 08:54
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.
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-17 08:56
(Received via mailing list)
From: Zhao Yi [mailto:youhaodeyi@gmail.com]
# 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
87349a7a95b3f2e83c20194ef122885c?d=identicon&s=25 Einar Magnús Boson (Guest)
on 2008-11-17 09:02
(Received via mailing list)
I find that overriding is much more powerful. You can override just
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
also requires static typing I think.



Einar Magnús Boson
+354-661 1649
einarmagnus@tistron.se
einar.boson@gmail.com
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2008-11-17 09:05
Peña, Botp wrote:
> From: Zhao Yi [mailto:youhaodeyi@gmail.com]
> # 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
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-17 09:09
(Received via mailing list)
From: Zhao Yi [mailto:youhaodeyi@gmail.com]
# 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

try it like,

def build directory=nil
  cd directory unless directory.nil?
  make
end
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2008-11-17 09:17
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
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-17 09:17
(Received via mailing list)
From: Peña, Botp [mailto:botp@delmonte-phil.com]
# def build directory=nil
#   cd directory unless directory.nil?
#   make
# end


or if you do not like the if/unless part,

def build directory="."
  cd directory
  make
end
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2008-11-17 14:44
(Received via mailing list)
On Nov 17, 2:26 am, Zhao Yi <youhaod...@gmail.com> wrote:
> end
> 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
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2008-11-17 15:00
(Received via mailing list)
Zhao Yi <youhaodeyi@gmail.com> writes:
> 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).
8666d1ebabcea440585dfe831a4af9f1?d=identicon&s=25 Brian Adkins (Guest)
on 2008-11-17 18:55
(Received via mailing list)
pjb@informatimago.com (Pascal J. Bourguignon) writes:

>>     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.
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2008-11-17 19:01
(Received via mailing list)
2008/11/17 Brian Adkins <lojicdotcom@gmail.com>

> >>
>
> 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
73ba5e17cf39ee5aa4ada6eefdb80177?d=identicon&s=25 Joe Wölfel (Guest)
on 2008-11-17 20:26
(Received via mailing list)
It's interesting that without the feature of overloading the code is
both shorter and more readable.
8666d1ebabcea440585dfe831a4af9f1?d=identicon&s=25 Brian Adkins (Guest)
on 2008-11-17 22:15
(Received via mailing list)
James Coglan <jcoglan@googlemail.com> writes:

>
> 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.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2008-11-17 23:46
(Received via mailing list)
On Nov 17, 2:22 pm, Joe Wölfel <j...@talkhouse.com> 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...

T.
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2008-11-18 00:40
(Received via mailing list)
Trans wrote:
> Hmm... I suppose that would require Ruby a bit more like Duby?
>
>   http://blog.headius.com/2008/03/duby-type-inferred...
>
> 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.
This topic is locked and can not be replied to.