How does overload work in ruby?

S

Shuaib Zahda

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
 
X

Xavier Noria

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

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
 
A

Austin Ziegler

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
 
B

Bertram Scharpf

Hi,

Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier Noria:
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
 
R

Rick DeNatale

Am Montag, 22. Okt 2007, 14:25:49 +0900 schrieb Xavier Noria:

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top