Multiple constructors

Hi,

Is there a way to have two constructors which take a different number of arguments?

If not, Ruby should really support multiple constructors!

Ciao,

-Lars

Lars M. wrote:

Hi,

Is there a way to have two constructors which take a different number of
arguments?

class Foo
def initialize(*args)
case args.size
when 1; init1(*args)
when 2; init2(*args)
end
end

def init1(arg)
p arg
end

def init2(arg1, arg2)
p arg1, arg2
end
end

Foo.new(1)
Foo.new(“a”,“b”)

Search ruby-talk archive (http://www.ruby-talk.org/ruby/ruby-talk/index.shtml) – you will find huge amount of discussions and solutions on the subject there.

Gennady.

···

----- Original Message -----
From: Lars M.
To: ruby-talk ML
Sent: Friday, January 17, 2003 12:17 PM
Subject: Multiple constructors …

Hi,

Is there a way to have two constructors which take a different number of arguments?

If not, Ruby should really support multiple constructors!

Ciao,

-Lars

(plaintext please!)

See the ‘Method Overloading’ section in

martin

···

Lars M. lars-m@gmx.net wrote:

[-- multipart/alternative, encoding 7bit, 48 lines --]