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 ???

···

--
Posted via http://www.ruby-forum.com/.

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

  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

···

2008/3/27, Selvaraj Subbaian <selvaraj@srishtisoft.com>:

--
use.inject do |as, often| as.you_can - without end

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.

···

2008/3/27, Selvaraj Subbaian <selvaraj@srishtisoft.com>:

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

--
Iñaki Baz Castillo
<ibc@aliax.net>

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 Gilcher

···

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