Over Loading

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

2008/3/27, Selvaraj S. [email protected]:

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

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

On Mar 27, 2008, at 10:34 AM, Иван Евтухович wrote:

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
arguments to some extend. (search for “match” on rubyforge)

Basically, they work by defining a proxy method that does the pattern
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 G.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

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

2008/3/27, Selvaraj S. [email protected]:

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

Better than an Array you can pass a Hash , so you keep the variable
name:

objet.my_method( { “num”=>2313 , “name”=>“My Name”,
“phone”=>"+34232343243" } )

def my_method ( variables = {} )
puts “My name is” + variables[“name”]
puts “My id num is” + variables[“num”].to_i
end

AFAIK RubyOnRails uses this way a lot.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs