same Methode name but different Parameter

S

Salai Khine

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

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.
 
T

Trans

Dear all,

can i use in Ruby the *same Methode name but different Parameter*??

like

def new (cent)

end

def new(euro,cent)

end.

def new(*args)
euro, cent = *args
...

T.
 
R

Rob Biedenharn

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
F

Francisco Laguna de la Vera

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 Khine:
 
R

Rick DeNatale

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 <G> in US Dollars would be:

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

2) use a hash argument to get the equivalent of keyword arguments

class Money
def initialize(&args={})
args = {:currency => :euros}.merge args # this
provides a default for currency
@currency = args[:currency]
@value = args[:value]
end
end

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 => :eek:ld_imperial_english, :shillings => 10, :pounds => 5)

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

Salai Khine

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

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 Newbie)



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 <G> in US Dollars would be:

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

2) use a hash argument to get the equivalent of keyword arguments

class Money
def initialize(&args={})
args = {:currency => :euros}.merge args # this
provides a default for currency
@currency = args[:currency]
@value = args[:value]
end
end

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 => :eek:ld_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/
 
F

Francisco Laguna de la Vera

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 Khine:
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 Newbie)



Dear all,

can i use in Ruby the *same Methode name but different Parameter*??

like

def new (cent)

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 <G> in US Dollars would be:

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

2) use a hash argument to get the equivalent of keyword arguments

class Money
def initialize(&args={})
args = {:currency => :euros}.merge args # this
provides a default for currency
@currency = args[:currency]
@value = args[:value]
end
end

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 => :eek:ld_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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top