Hello guys
I was trying to overload one method but it does not work like Java.
I noticed it only uses the first method that it meets.
is there any special way for overloading in ruby.
class Overloading
def sum(a, b)
return a + b
end
def sum(a, b, c)
return a + b + c
end
end
obj = Overloading.new
puts obj.sum(5, 7)
puts obj.sum(5, 6, 7)
the output
overloading.rb:13:in `sum’: wrong number of arguments (2 for 3)
(ArgumentError)
from overloading.rb:13
any idea?
Regards
Shuaib
On Oct 22, 2007, at 7:17 AM, Shuaib Z. wrote:
return a + b
end
def sum(a, b, c)
return a + b + c
end
Ruby does not support method overloading that way. As you founded out
only one of the definitions of sum survives. However, you can define
defaults for optional parameters like this:
def sum(a, b, c=0)
a + b + c
end
– fxn
On 10/22/07, Shuaib Z. [email protected] wrote:
I was trying to overload one method but it does not work like Java.
I noticed it only uses the first method that it meets.
There is no overloading in Ruby, only overriding, and the last
definition overrides any previous definition.
The case for what you’re wanting is actually not that hard:
class Summation
def sum(*args)
args.inject { |a, i| a + i }
end
end
As you can see, for this particular sort of behaviour, you don’t even
need a method. *args, as well, allows for “infinite” arguments.
Specific arguments can be reached with:
class Summation
def sum(first, *rest)
rest.inject(first) { |a, i| a + i }
end
end
-austin
Hi,
Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier N.:
return a + b + c
end
Ruby does not support method overloading that way. […]
However, you can define defaults
for optional parameters like this:
def sum(a, b, c=0)
a + b + c
end
If you want to allow the caller to select the default value
explicitly you may say
def sum a, b, c = nil
c ||= 0
a + b + c
end
Some methods even change their behaviour with the number of
parameters they are called with, for example File#basename.
You still may check whether the last parameter is nil. If
you need to be 100% sure, you could write a C function;
rb_scan_args() returns the number of arguments given.
Bertram
On 10/22/07, Bertram S. [email protected] wrote:
Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier N.:
end
It seems to me that this is just a slightly less efficient way of
doing the same thing as the code you quoted.
In either case the caller isn’t specifying the default value, it’s
overriding the default.
In the call sum(1,2) ruby will evaluate the default expression for c,
in the call sum(1,2,3) it won’t since c has a value given by the third
parameter. Making the default value of c nil, and then doing c ||= 0
justs slows down both cases.
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
On Mon, 2007-10-22 at 21:31 +0900, Rick DeNatale wrote:
In either case the caller isn’t specifying the default value, it’s
overriding the default.
In the call sum(1,2) ruby will evaluate the default expression for c,
in the call sum(1,2,3) it won’t since c has a value given by the third
parameter. Making the default value of c nil, and then doing c ||= 0
justs slows down both cases.
It also means that if the caller really wants to pass nil, it’ll get
turned to the default value.