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
···
--
Posted via http://www.ruby-forum.com/.
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 Oct 22, 2007, at 7:17 AM, Shuaib Zahda wrote:
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
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
···
On 10/22/07, Shuaib Zahda <shuaib.zahda@gmail.com> 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.
--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
* austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
* austin@zieglers.ca
Hi,
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
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
···
Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier Noria:
On Oct 22, 2007, at 7:17 AM, Shuaib Zahda wrote:
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
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.
···
On 10/22/07, Bertram Scharpf <lists@bertram-scharpf.de> wrote:
Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier Noria:
> 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
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
It also means that if the caller really wants to pass nil, it'll get
turned to the default value.
- Arlen
···
On Mon, 2007-10-22 at 21:31 +0900, Rick DeNatale wrote:
> 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
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.