[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

method overloading like Java?

Daniel Finnie

12/4/2006 2:37:00 AM

In Java, you could write the following code:
public void foo(int x)
{ /* do something*/ }

public void foo(String x)
{ /* do something*/ }

int x1
String x2

foo(x1)
foo(x2)

The 2nd to last line would call the first foo and the last line would
call the 2nd foo.

Is there a way to do this in Ruby? I am aware of method(*args) and
case...when but I think Java's version is cleaner and more readable. I
also know that variables in Ruby are dynamically typed, the method that
is called should be based on the most recent type of the variable being
passed.

I don't want to do this:
def foo(arg)
if arg.type_of? String
# do something
elsif arg.type_of? Numeric
#do something
else
raise args error
end
end

thanks,
Dan

8 Answers

dblack

12/4/2006 3:23:00 AM

0

Tim Hunter

12/4/2006 3:25:00 AM

0

Daniel Finnie wrote:
> In Java, you could write the following code:
> public void foo(int x)
> { /* do something*/ }
>
> public void foo(String x)
> { /* do something*/ }
>
> int x1
> String x2
>
> foo(x1)
> foo(x2)
>
> The 2nd to last line would call the first foo and the last line would
> call the 2nd foo.
>
> Is there a way to do this in Ruby? I am aware of method(*args) and
> case...when but I think Java's version is cleaner and more readable.
> I also know that variables in Ruby are dynamically typed, the method
> that is called should be based on the most recent type of the variable
> being passed.
>
> I don't want to do this:
> def foo(arg)
> if arg.type_of? String
> # do something
> elsif arg.type_of? Numeric
> #do something
> else
> raise args error
> end
> end
>
> thanks,
> Dan
>
Java's (and C++'s) kind of method overloading just isn't do-able in
Ruby. Generally, if you have a method that can take either a String or a
Numeric argument, then you create two methods, one that takes a String
argument and another for the Numeric argument.

Switching on the class of an argument is also regarded with suspicion
but is acceptable in some cases. Search for "duck typing" in the Pickaxe
or in the archives of this mailing list for exhaustive discussions.


Gordon Thiesfeld

12/4/2006 3:41:00 AM

0

> Is there a way to do this in Ruby? I am aware of method(*args) and
> case...when but I think Java's version is cleaner and more readable. I
> also know that variables in Ruby are dynamically typed, the method that
> is called should be based on the most recent type of the variable being
> passed.

What about this?

class String
def foo
puts 'String'
end
end

class Numeric
def foo
puts 'Numeric'
end
end

x1 = 1
x2 = 'string'

x1.foo
x2.foo

Devin Mullins

12/4/2006 3:42:00 AM

0

Daniel Finnie wrote:
> I don't want to do this:
> def foo(arg)
> if arg.type_of? String
> # do something
> elsif arg.type_of? Numeric
> #do something
> else
> raise args error
> end
> end
1. That's the only way to do it.

2. You'd be better served if you considered code like that a smell. If
you've got a method whose functionality varies _vastly_ based on the
type of its arguments, then they probably deserve to be separate
methods, with more descriptive names.

Devin

Pat Maddox

12/4/2006 3:51:00 AM

0

On 12/3/06, Gordon Thiesfeld <gthiesfeld@gmail.com> wrote:
> > Is there a way to do this in Ruby? I am aware of method(*args) and
> > case...when but I think Java's version is cleaner and more readable. I
> > also know that variables in Ruby are dynamically typed, the method that
> > is called should be based on the most recent type of the variable being
> > passed.
>
> What about this?
>
> class String
> def foo
> puts 'String'
> end
> end
>
> class Numeric
> def foo
> puts 'Numeric'
> end
> end
>
> x1 = 1
> x2 = 'string'
>
> x1.foo
> x2.foo

EXACTLY

If you want different types of objects to perform different behavior
for the same method call, you use polymorphism. What OP wants to do
is essentially a design pattern for providing polymorphism
capabilities to a language that doesn't have them built in.

Pat

Austin Ziegler

12/4/2006 4:35:00 AM

0

On 12/3/06, Yang Bob <bob.yang.dev@gmail.com> wrote:
> But how about overloading by parameter numbers. such as:
> class FooClass {
> def foo(first){ }
> def foo(first, second) { }
> }
>
> does ruby support this ? if can not, how can we workaround this?

class Foo
def foo(first, second = nil)
end
end

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Max Muermann

12/4/2006 4:53:00 AM

0

On 12/4/06, Yang Bob <bob.yang.dev@gmail.com> wrote:
> But how about overloading by parameter numbers. such as:
> class FooClass {
> def foo(first){ }
> def foo(first, second) { }
> }
>
> does ruby support this ? if can not, how can we workaround this?
>
> thanks
>

There's also this one:

def foo opts={}
if opts[:some_parameter]
do_something_with_parameter
end

if opts[:some_other_parameter]
do_something_with_other_parameter
end
end

foo :some_parameter=>123, :some_other_parameter=>'abc'

This is used as a substitute for 'true' named parameters. It is mostly
useful if you need to take a moderate number of entirely optional
parameters.

Cheers,
Max

Robert Klemme

12/4/2006 8:24:00 AM

0

On 04.12.2006 03:37, Daniel Finnie wrote:
> In Java, you could write the following code:
> public void foo(int x)
> { /* do something*/ }
>
> public void foo(String x)
> { /* do something*/ }

You can emulate it - although I am not convinced that it is a good idea.

http://wiki.rubygarden.org/Ruby/page/show/MethodO...
http://wiki.rubygarden.org/Ruby/page/show/R...

Kind regards

robert