Hey ..
I'm Tryin' to port Some Java Module into Ruby , but I have a small
probleme ,
how can I do to declare multi 'initialize Method' depending of the
number of user
argument
i.e in Java we can have a Class with many constructor
class myClass {
//...
public MyClass () { ... } // Defualt One
public MyClass (Object obj1 ) { ... }
public myClass ( Object obj1,Object obj2 ...,Object objn) { ... }
// ...
}
I tried :
class myClass
def initialize ()
// ...
end
def intialize ( value )
// ...
end
end
but doesnt works !
I just wanna find a way to call the right constructor ( or initializer)
depending of the number of arguments
On Wed, Jan 27, 2010 at 4:34 PM, Mido Peace <mido.peace@gmail.com> wrote:
Hey ..
I'm Tryin' to port Some Java Module into Ruby , but I have a small
probleme ,
how can I do to declare multi 'initialize Method' depending of the
number of user
argument
i.e in Java we can have a Class with many constructor
class myClass {
//...
public MyClass () { ... } // Defualt One
public MyClass (Object obj1 ) { ... }
public myClass ( Object obj1,Object obj2 ...,Object objn) { ... }
// ...
}
I tried :
class myClass
def initialize ()
// ...
end
def intialize ( value )
// ...
end
end
but doesnt works !
I just wanna find a way to call the right constructor ( or initializer)
depending of the number of arguments
The first one is this equivalent of vaargs in C language
def initialize(*args)
args.each do |a|
puts a.class.inspect
end
end
The second one is to pass a hash or an array in parameter.
And the third is to put default values to your args:
def initialize(var1='', var2=0, var3=nil)
end
Hope i helped !
Kind regards
Mido Peace wrote:
···
Hey ..
I'm Tryin' to port Some Java Module into Ruby , but I have a small
probleme ,
how can I do to declare multi 'initialize Method' depending of the
number of user
argument
i.e in Java we can have a Class with many constructor
class myClass {
//...
public MyClass () { ... } // Defualt One
public MyClass (Object obj1 ) { ... }
public myClass ( Object obj1,Object obj2 ...,Object objn) { ... }
// ...
}
I tried :
class myClass
def initialize ()
// ...
end
def intialize ( value )
// ...
end
end
but doesnt works !
I just wanna find a way to call the right constructor ( or initializer)
depending of the number of arguments
A solution like this was proposed for overloading once but it does not
seem to be widely used:
irb(main):001:0> def overload(*a)
irb(main):002:1> case a.map {|x|x.class}
irb(main):003:2> when [String]
irb(main):004:2> puts "a single string"
irb(main):005:2> when [Fixnum, String]
Cool. Where is "Array#===" documented? I couldn't find it.
Unfortunately Array#=== is just the same as Array#==, which is
why the argument list is mapped to class objects via #map in that
example from Robert.
There was a somewhat recent thread about changing Array#=== to
be an element-wise application of ===, which makes a lot of
sense to me.
Gary Wright
···
On Jan 27, 2010, at 4:14 PM, Albert Schlef wrote:
Robert Klemme wrote:
A solution like this was proposed for overloading once but it does not
seem to be widely used:
irb(main):001:0> def overload(*a)
irb(main):002:1> case a.map {|x|x.class}
irb(main):003:2> when [String]
irb(main):004:2> puts "a single string"
irb(main):005:2> when [Fixnum, String]
Cool. Where is "Array#===" documented? I couldn't find it.