Over Loading

S

Selvaraj Subbaian

Hai All,

Question looks like simple Why ruby is not supporting over loading??

I think we can pass any number of argument as an array to method. is
this the reason or any other ???
 
É

é×ÁÎ å×ÔÕÈÏ×ÉÞ

Because of Ruby type system

You cant write

def test(int i)

end

def test(string s)

end

In Ruby this is
def test(arg)
end

So you can do something like this
def test(arg)
if arg.is_a?(String)
else
end
end
 
R

Robert Klemme

2008/3/27 said:
Question looks like simple Why ruby is not supporting over loading??

You would have to ask Matz for details. Here are some potential reasons:

- Ruby is dynamically typed which makes overloading based on argument
types impossible; you could only overload on argument list structure
(mostly number of args)

- argument list structure based overloading is seldom useful and in
those cases the implementer can do it himself - no need to burden the
language with this.

- because of Ruby's dynamic nature methods that are overloaded based
on argument type in other languages often fall into one and need not
be overloaded.
I think we can pass any number of argument as an array to method. is
this the reason or any other ???

This might be part of the reason (see above).

Kind regards

robert
 
F

Florian Gilcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


So you can do something like this
def test(arg)
if arg.is_a?(String)
else
end
end


There are some projects out there that allow pattern matching on =20
arguments to some extend. (search for "match" on rubyforge)

Basically, they work by defining a proxy method that does the pattern =20=

matching and then calling some predefined procs. Essentially,
they define this test-Method dynamically.
This is not overloading in a strict sense, but it comes close.

Greetings
Florian Gilcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkfrk3MACgkQJA/zY0IIRZZImgCeLw1Db8i/wyhlNP+0jrlXpjUD
RaUAn0ecOSw8Qd5wOzmwI/LHkL5rBncb
=3D/pKG
-----END PGP SIGNATURE-----
 
I

Iñaki Baz Castillo

MjAwOC8zLzI3LCBTZWx2YXJhaiBTdWJiYWlhbiA8c2VsdmFyYWpAc3Jpc2h0aXNvZnQuY29tPjoK
PiAgSSB0aGluayB3ZSBjYW4gcGFzcyBhbnkgbnVtYmVyIG9mIGFyZ3VtZW50IGFzIGFuIGFycmF5
IHRvIG1ldGhvZC4gaXMKPiAgdGhpcyB0aGUgcmVhc29uIG9yIGFueSBvdGhlciA/Pz8KCkJldHRl
ciB0aGFuIGFuIEFycmF5IHlvdSBjYW4gcGFzcyBhIEhhc2ggLCBzbyB5b3Uga2VlcCB0aGUgdmFy
aWFibGUgbmFtZToKCm9iamV0Lm15X21ldGhvZCggeyAibnVtIj0+MjMxMyAsICJuYW1lIj0+Ik15
IE5hbWUiLCAicGhvbmUiPT4iKzM0MjMyMzQzMjQzIiB9ICkKCgpkZWYgbXlfbWV0aG9kICggdmFy
aWFibGVzID0ge30gKQoJcHV0cyAiTXkgbmFtZSBpcyIgKyB2YXJpYWJsZXNbIm5hbWUiXQoJcHV0
cyAiTXkgaWQgbnVtIGlzIiArIHZhcmlhYmxlc1sibnVtIl0udG9faQplbmQKCkFGQUlLIFJ1YnlP
blJhaWxzIHVzZXMgdGhpcyB3YXkgYSBsb3QuCgoKLS0gCknDsWFraSBCYXogQ2FzdGlsbG8KPGli
Y0BhbGlheC5uZXQ+Cg==
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top