[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

two initialize methods ?

pere.noel

1/16/2006 10:08:00 AM

is it possible, in Ruby, as in Java to define two initialize methods
with dirrent type of args ?

seems not :

class AddressBook
attr_accessor :name, :phone, :address
def initialize( n, p, a)
@name = n
@phone = p
@address = a
end
def initialize( h)
@name = h['name']
@phone = h['phone']
@address = h['address']
end
end

toto = AddressBook.new("Toto", "01 23 45 67 89", "12 rue Machin 75017
Paris")


leds to :
ArgumentError: wrong number of arguments (3 for 1)

then it seems the last initialize is taken

i'd like to construct an object either from hash or sequence...
--
une bévue
2 Answers

Robert Klemme

1/16/2006 10:11:00 AM

0

Une bévue wrote:
> is it possible, in Ruby, as in Java to define two initialize methods
> with dirrent type of args ?
>
> seems not :
>
> class AddressBook
> attr_accessor :name, :phone, :address
> def initialize( n, p, a)
> @name = n
> @phone = p
> @address = a
> end
> def initialize( h)
> @name = h['name']
> @phone = h['phone']
> @address = h['address']
> end
> end
>
> toto = AddressBook.new("Toto", "01 23 45 67 89", "12 rue Machin 75017
> Paris")
>
>
> leds to :
> ArgumentError: wrong number of arguments (3 for 1)
>
> then it seems the last initialize is taken

Exactly.

> i'd like to construct an object either from hash or sequence...

You can find some idioms for this here:

http://www.rubygarden.org/ruby?MethodO...
http://www.rubygarden.org/ruby?R...

Kind regards

robert

pere.noel

1/16/2006 12:12:00 PM

0

Robert Klemme <bob.news@gmx.net> wrote:

> ou can find some idioms for this here:
>
> http://www.rubygarden.org/ruby?MethodO...
> http://www.rubygarden.org/ruby?R...

ok, thanks, i see a solution...
--
une bévue