[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Methods names mangled with number of arguments

Mystifier

1/27/2005 6:19:00 PM

Hi

The current situation in Ruby with method calls is

class A
def meth(arg1)
end
end

class B < A
def meth(arg1,arg2)
end
end

a = A.new
b = B.new

b.meth(1,2) is fine but a.meth(1,2) . This is fine but what one assumes is
that when a method is overridden, you wish to override the implementation.
In that case, this is a problem.

My suggestion is 2 options.

Option 1:
Make all the methods as variable arguments. If a method is get more
arguments then it uses, it can safely ignore the extra ones. In case it gets
lesser ones (I am not sure here) , it may take the values as nil.
I do not think this is a good option but messaging based languages like Ruby
might prefer them.

Option 2:
Mangle method names with number of arguments. meth in class A becomes meth_1
and meth in class B becomes meth_2. So, B is not actually overriding the
meth.

regards,
Mystifier

http://vuby....



1 Answer

Gavin Kistner

2/10/2005 2:37:00 PM

0

On Jan 27, 2005, at 11:18 AM, Mystifier wrote:
> The current situation in Ruby with method calls is
>
> class A
> def meth(arg1)
> end
> end
>
> class B < A
> def meth(arg1,arg2)
> end
> end
>
> a = A.new
> b = B.new
>
> b.meth(1,2) is fine but a.meth(1,2) . This is fine but what one
> assumes is
> that when a method is overridden, you wish to override the
> implementation.
> In that case, this is a problem.

If you wish to override the implementation, then you should do so:

class A
def meth(arg1)
end
end

#...later...
class A
def meth(arg1,arg2)
end
end


It would not make sense to affect/change a parent class that you
inherit from. Consider:

class Animal
def eat
gobble_ravenously()
end
end

class Person < Animal
def eat
use_fork_and_knife()
end
end

If the 'redefinition' of #eat by the Person class affected the Animal
class, all animals would suddenly be using cutlery.

Am I misunderstanding you?