From: Mido Peace on
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


Thx ;)
--
Posted via http://www.ruby-forum.com/.

From: Jesús Gabriel y Galán on
On Wed, Jan 27, 2010 at 4:34 PM, Mido Peace <mido.peace(a)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

Ruby doesn't support method overloading. If you need different
implementations just depending on the number of arguments you can do:

class MyClass
def initialize *args
case args.size
when 1
_init_1_param *args
when 2
_init_2_params *args
....
end
end

or you can do this:

class MyClass
def initialize(options = {})
# and have the logic depend on the keys present in the hash
end
end

So you can call:

MyClass.new
MyClass.new(:obj1 => some_object, :obj2 => some_other_object)

Hope this helps,

Jesus.

From: Jesús Gabriel y Galán on
Checking old emails on the matter, I like this approach a lot:

If you have different ways of constructing objects of a class, create
different class methods to do so. For example:

class MyClass
def self.from_x_y(x,y)
MyClass.new(x,y)
end

def self.default
MyClass.new(0,0)
end

def self.from_point(p)
MyClass.new(p.x, p.y)
end

def initialize(x,y)
@x = x
@y = y
end
end

This way you can have:

MyClass.default #=> 0,0
MyClass.from_x_y(10,20) # => 10,20
MyClass.from_point(Point.new(1,2)) #=> 1,2

Hope this helps,

Jesus.

From: Pierre Lecocq on
Hello Mido,

There are several solutions to this problem.

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
>
>
> Thx ;)

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

From: Robert Klemme on
On 27.01.2010 16:58, Pierre Lecocq wrote:
> Hello Mido,
>
> There are several solutions to this problem.
>
> 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 !

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]
irb(main):006:2> puts a[1] * a[0]
irb(main):007:2> else
irb(main):008:2* raise ArgumentError, "can't %p" % a
irb(main):009:2> end
irb(main):010:1> end
=> nil
irb(main):011:0> overload "foo"
a single string
=> nil
irb(main):012:0> overload 2, "foo"
foofoo
=> nil
irb(main):013:0> overload 2
ArgumentError: can't 2
from (irb):8:in `overload'
from (irb):13
from /usr/local/bin/irb19:12:in `<main>'
irb(main):014:0>

You can even shorten that to

case a.map(&:class)
....

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/