# Same Methode name but different Parameter

**URL:** <https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899>\
**Category:** Ruby\
**Created:** [January 3, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899 "2008-01-03T16:54:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [January 3, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899/1 "2008-01-03T16:54:32Z")

</div>

Dear all,

can i use in Ruby the _same Methode name but different Parameter_??

like

def new (cent)

end

def new(euro,cent)

end.

regards,

salai.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [January 3, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899/2 "2008-01-03T17:17:33Z")

</div>

On Jan 3, 10:53 am, “Salai K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def new(euro,cent)
> 
> end.

def new(\*args)  
euro, cent = \*args  
…

T.

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [January 3, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899/3 "2008-01-03T17:34:09Z")

</div>

On Jan 3, 2008, at 11:16 AM, Trans wrote:

> > def new(euro,cent)
> > 
> > end.
> 
> def new(\*args)  
> euro, cent = \*args  
> …
> 
> T.

or something that will actually do what you seem to want:

def initialize(\*args)  
args.unshift nil if args.size == 1  
euro, cent = \*args  
…  
end

You typically don’t define “new” for a class, but often define  
“initialize” which is called by ruby after allocating the space for  
your new instance.

You’ll find it much more common in ruby to have optional trailing  
arguments with default values:

def initialize(cent, euro=nil)  
…  
end

Then you’d have YourClass.new(centvalue, eurovalue) or just  
YourClass.new(centvalue) and let the euro default to nil.

When things get more complicated than an optional argument or two, an  
options hash tends to take over. This is particularly easy to do as  
ruby lets you include a hash of key =\> value pairs at the end of an  
argument list without extra {} to introduce the literal hash.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [January 3, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899/4 "2008-01-03T17:35:20Z")

</div>

Hi Salai!

I’m afraid you can’t. The usual way for overloading a method in this  
way is to specify default values for optional parameters:

def new(cent, euro=0)  
…  
end

with the downside that you’ll have to add the optional parameters to  
the end of the parameter list. As a side note, I’d be confused by  
methods called ‘new’, and would constantly confuse them with  
constructors. But that’s just me I guess.

Gentle Breezes  
Cisco

Am 03.01.2008 um 16:53 schrieb Salai K.:

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [January 3, 2008, 7:53pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899/5 "2008-01-03T19:53:45Z")

</div>

Dear all,

Thankyou very much. We have finally solve problem like below. I think it  
is  
not the best way.

class Euro

def Euro.new\_from\_cent\_only (cent)  
@cent = cent  
end

def Euro.new\_from\_euro\_cent (euro,cent)  
@cent = (euro \* 100) + cent  
end

def Euro.new\_from\_internal\_value (an\_internal\_value)  
Euro.new\_from\_cent\_only(an\_internal\_value)  
end  
end

test1 = Euro.new\_from\_cent\_only(10)  
test2 = Euro.new\_from\_euro\_cent(11,20)  
test3 = Euro.new\_from\_internal\_value(200)  
test4 = Euro.new\_from\_internal\_value(test3)  
test5 = Euro.new\_from\_internal\_value(200)

puts test1  
puts test2 === test3  
puts test3 === test5 \<---- true  
puts test4.object\_id  
puts test5.object\_id

now, we have another problem. How can we test … that  
test3  
test5  
have differents object. ???

regards,

salai.  
(Ruby N.)

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [January 3, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899/6 "2008-01-03T17:44:08Z")

</div>

On Jan 3, 2008 11:16 AM, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> > 
> > def new(euro,cent)
> > 
> > end.
> 
> def new(\*args)  
> euro, cent = \*args  
> …

Well, this didn’t really answer the OPs question, and is a bit  
misleading.

calling this with one argument will set euro to ni, and cent to the  
argument, which probably isn’t what’s looked for.

Answering more directly, no Ruby methods are named only by the name,  
and there’s no notion of overloading with different parameter types.

First an aside, since you named this method new, I’m guessing that you  
are talking about creating a new object, this is (probably) another  
difference between Ruby and whatever language you have used before.  
In ruby the new method is almost never overriden (and it’s a class  
method anyway so it would be def self.new;end). Instead new objects  
are intialized through an instance method called initialize.

Okay, that said there are various techniques to allow for some  
variation in parameters to a method.

One is the use of a final argument prefixed by \* this collects any  
arguments passed left over after any prior arguments are satisfied to  
an array. So let’s say you were doing a class representing Money  
valuated in Euros, and that you wanted EuroMoney.new(10) to create an  
object containing 10 Euro cents, and EuroMoney.new(1,50) to represent  
1 and 1/2 Euros. You could do this with something like

class EuroMoney  
def initialize(\*args)  
raise ArgumentError unless (1…2).include?(args.length)  
@cents = args.last  
if args.length \> 1  
@euros = args.first  
else  
@euros = 0  
end  
end  
end

Now lets’ say instead we want to be more flexible and have a money  
class which has both a value and a currency, and you want to specify a  
unit amount, and a currency with a default. There are at least two  
ways to do this:

1. currency argument with default value:

class Money  
def initialize(value, currency = :euros)  
@value, @currency = value, currency  
end  
end

So 1.50 Euros could be created either with:

```
  Money.new(150)

```

or  
Money.new(150, :euros)

and the equivalent in US Dollars would be:

```
 Money.new(1500, :"US$")

```

1. use a hash argument to get the equivalent of keyword arguments

A formal parameter with an & prefix is a hash.

So we would have

Money.new(:value =\> 150)  
Money.new(:currency =\> :euros, :value =\> 150)  
and  
Money.new(:currency =\> :“US$”, :value =\> 1500)

as the equivalents to the previous examples.

The second approach is probably overkill, on the other hand, with some  
more coding, one could use it to model more general concepts of money  
with more code in the initialize method and support things like:

Money.new(:currency =\> :old\_imperial\_english, :shillings =\> 10,  
:pounds =\> 5)

which would compute the unit value based on the components in a flexible  
manner.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![koko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@koko](https://www.ruby-forum.com/u/koko)\
**Post date:** [January 3, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/same-methode-name-but-different-parameter/124899/7 "2008-01-03T20:24:25Z")

</div>

Aloha!

I’m afraid what you wrote there doesn’t quite do what you expected.  
Some pointers on what (I think) happens:

class Euro  
def Euro.new\_from\_cent\_only (cent)  
@cent = cent  
end  
end

To understand whats going on, you must know two little things, that go  
a long way:

1. A method returns the value of the last line of the method. In this  
case that means the value of “@cent = cent”.  
and
2. An assignment simply has as its value the assigned value (so you  
can write “a = b = c = 3”), which means the method simply returns its  
argument.

Euro.new\_fron\_cent\_only(10) will return the Fixnum instance for 10.

The method also doesn’t create an instance of your class. You need an  
#initialize method for that.

Another way you might want to go about this:

class Euro  
def initialize(cent)  
@cent = cent  
end

def Euro.new\_from\_cent\_only(cent)  
Euro.new(cent)  
end

def Euro.new\_from\_euro\_cent(euro, cent)  
Euro.new((euro\*100) + cent)  
end  
end

Though this certainly feels a bit clumsy. Better go for the afore  
mentioned trick with the \*args.  
So if you want something weirder you could add a euros method to Float

class Float  
def euros  
Euro.new((self\*100).to\_i)  
end  
end

So you can write “42.50.euros”

Gentle Breezes  
Cisco

Am 03.01.2008 um 19:52 schrieb Salai K.:
