# How can I overload a method in Ruby

**URL:** <https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206>\
**Category:** ruby-talk\
**Created:** [17 November 2008 07:26 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206 "2008-11-17T07:26:27Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [17 November 2008 07:26 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/1 "2008-11-17T07:26:27Z")

</div>

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

&nbsp;&nbsp;def fun(directory)  
&nbsp;&nbsp;&nbsp;&nbsp;puts ("fun directory")  
&nbsp;&nbsp;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)  
&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [17 November 2008 07:42 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/2 "2008-11-17T07:42:29Z")

</div>

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:

> **···**
>
> On Nov 16, 2008, at 23:26 , Zhao Yi wrote:
> 
> > def fun dir = nil  
> > &nbsp;&nbsp;puts "fun #{dir}"  
> > end

---

<div class="post-metadata">

**Author:** ![Einar\_Boson](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@Einar\_Boson](https://rubytalk.org/u/Einar_Boson)\
**Post date:** [17 November 2008 07:46 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/3 "2008-11-17T07:46:25Z")

</div>

there is no overloading, only overriding.

You can have variable number of arguments by writing

def fun(\*params)  
&nbsp;&nbsp;&nbsp;#here params is an array of your arguments, if none.  
end

you can also have a default value for your parameter:

class Impl  
&nbsp;&nbsp;include MyModule

&nbsp;&nbsp;def fun(directory = "")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "fun #{directory}"  
&nbsp;&nbsp;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()  
> > &nbsp;&nbsp;&nbsp;puts ("fun")  
> > end
> > 
> > def fun(directory)  
> > &nbsp;&nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [17 November 2008 13:40 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/4 "2008-11-17T13:40:54Z")

</div>

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

&nbsp;&nbsp;require 'facets/overload'

&nbsp;&nbsp;class X  
&nbsp;&nbsp;&nbsp;&nbsp;def x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hello"  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;overload :x, Integer do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;overload :x, String, String do |s1, s2|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[s1, s2]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

> **···**
>
> On Nov 17, 2:26 am, Zhao Yi \<youhaod...@gmail.com\> 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?

---

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [17 November 2008 07:47 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/5 "2008-11-17T07:47:12Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [17 November 2008 07:50 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/6 "2008-11-17T07:50:46Z")

</div>

Einar Magnús Boson wrote:

> there is no overloading, only overriding.
> 
> You can have variable number of arguments by writing
> 
> def fun(\*params)  
> &nbsp;&nbsp;&nbsp;#here params is an array of your arguments, if none.  
> end
> 
> you can also have a default value for your parameter:
> 
> class Impl  
> &nbsp;&nbsp;include MyModule
> 
> &nbsp;&nbsp;def fun(directory = "")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "fun #{directory}"  
> &nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [17 November 2008 07:52 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/7 "2008-11-17T07:52:34Z")

</div>

# 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

> **···**
>
> From: Zhao Yi [[mailto:youhaodeyi@gmail.com](mailto:youhaodeyi@gmail.com)]

---

<div class="post-metadata">

**Author:** ![Einar\_Boson](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@Einar\_Boson](https://rubytalk.org/u/Einar_Boson)\
**Post date:** [17 November 2008 07:57 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/8 "2008-11-17T07:57:53Z")

</div>

I find that overriding is much more powerful. You can override just about any method.  
Try

class Fixnum  
&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;"this is kinda stupid"  
&nbsp;&nbsp;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

> **···**
>
> On 17.11.2008, at 07:50 , Zhao Yi wrote:
> 
> > Einar Magnús Boson wrote:
> > 
> > > there is no overloading, only overriding.
> > > 
> > > You can have variable number of arguments by writing
> > > 
> > > def fun(\*params)  
> > > &nbsp;&nbsp;#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 = "")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [17 November 2008 08:02 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/9 "2008-11-17T08:02:13Z")

</div>

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()  
&nbsp;&nbsp;&nbsp;&nbsp;make  
end

def build(directory)  
&nbsp;&nbsp;&nbsp;&nbsp;cd directory  
&nbsp;&nbsp;&nbsp;&nbsp;make  
end

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [17 November 2008 08:05 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/10 "2008-11-17T08:05:15Z")

</div>

# def build()  
# make  
# end

> **···**
>
> From: Zhao Yi [[mailto:youhaodeyi@gmail.com](mailto:youhaodeyi@gmail.com)]  
> #  
> # 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  
> &nbsp;&nbsp;cd directory unless directory.nil?  
> &nbsp;&nbsp;make  
> end

---

<div class="post-metadata">

**Author:** ![Pascal\_Bourguignon](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@Pascal\_Bourguignon](https://rubytalk.org/u/Pascal_Bourguignon)\
**Post date:** [17 November 2008 13:56 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/11 "2008-11-17T13:56:39Z")

</div>

Zhao Yi \<youhaodeyi@gmail.com\> 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()  
> &nbsp;&nbsp;&nbsp;&nbsp;make  
> end
> 
> def build(directory)  
> &nbsp;&nbsp;&nbsp;&nbsp;cd directory  
> &nbsp;&nbsp;&nbsp;&nbsp;make  
> end

Call it: buildWithDirectory(directory)

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

If you insist on one name for both, then:

def build(directory=nil)  
&nbsp;&nbsp;...  
end

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

> **···**
>
> --  
> \_\_Pascal Bourguignon\_\_

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [17 November 2008 08:12 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/12 "2008-11-17T08:12:43Z")

</div>

# def build directory=nil  
# cd directory unless directory.nil?  
# make  
# end

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

def build directory="."  
&nbsp;&nbsp;cd directory  
&nbsp;&nbsp;make  
end

> **···**
>
> From: Peña, Botp [[mailto:botp@delmonte-phil.com](mailto:botp@delmonte-phil.com)]

---

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [17 November 2008 08:13 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/13 "2008-11-17T08:13:48Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [17 November 2008 17:51 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/14 "2008-11-17T17:51:40Z")

</div>

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> Zhao Yi \<youhaodeyi@gmail.com\> 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()  
> > &nbsp;&nbsp;&nbsp;&nbsp;make  
> > end
> > 
> > def build(directory)  
> > &nbsp;&nbsp;&nbsp;&nbsp;cd directory  
> > &nbsp;&nbsp;&nbsp;&nbsp;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://www.lojic.com/)
> 
> > **[Lojic Technologies Blog](https://blog.lojic.com//)**
> >
> > Lojic Technologies Blog

---

<div class="post-metadata">

**Author:** ![Joe\_Wolfel](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joe\_Wolfel](https://rubytalk.org/u/Joe_Wolfel)\
**Post date:** [17 November 2008 19:22 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/15 "2008-11-17T19:22:02Z")

</div>

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

> **···**
>
> On 17 nov. 08, at 03:05, Peña, Botp wrote:
> 
> > 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  
> > &nbsp;&nbsp;cd directory unless directory.nil?  
> > &nbsp;&nbsp;make  
> > end

---

<div class="post-metadata">

**Author:** ![James\_Coglan2](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James\_Coglan2](https://rubytalk.org/u/James_Coglan2)\
**Post date:** [17 November 2008 17:56 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/16 "2008-11-17T17:56:15Z")

</div>

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

def build(directory = nil)  
&nbsp;&nbsp;cd directory if directory  
&nbsp;&nbsp;make  
end

> **···**
>
> 2008/11/17 Brian Adkins \<lojicdotcom@gmail.com\>
> 
> > pjb@informatimago.com (Pascal J. Bourguignon) writes:
> > 
> > \> Zhao Yi \<youhaodeyi@gmail.com\> 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.

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [17 November 2008 22:42 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/17 "2008-11-17T22:42:08Z")

</div>

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?

&nbsp;&nbsp;[http://blog.headius.com/2008/03/duby-type-inferred-ruby-like-jvm.html](http://blog.headius.com/2008/03/duby-type-inferred-ruby-like-jvm.html)

T.

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [17 November 2008 21:11 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/18 "2008-11-17T21:11:39Z")

</div>

James Coglan \<jcoglan@googlemail.com\> writes:

> [Note: parts of this message were removed to make it a legal post.]
> 
> > pjb@informatimago.com (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)  
> &nbsp;&nbsp;cd directory if directory  
> &nbsp;&nbsp;make  
> end

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

> **···**
>
> > 2008/11/17 Brian Adkins \<lojicdotcom@gmail.com\>
> 
> --  
> Brian Adkins  
> [http://www.lojic.com/](http://www.lojic.com/)
> 
> > **[Lojic Technologies Blog](https://blog.lojic.com//)**
> >
> > Lojic Technologies Blog

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [17 November 2008 23:36 UTC](https://rubytalk.org/t/how-can-i-overload-a-method-in-ruby/50206/19 "2008-11-17T23:36:43Z")

</div>

Trans 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?
> 
> &nbsp;&nbsp;[http://blog.headius.com/2008/03/duby-type-inferred-ruby-like-jvm.html](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.

> **···**
>
> > On Nov 17, 2:22 pm, Joe Wölfel \<j...@talkhouse.com\> wrote:
