Ross Bamford
5/13/2006 2:37:00 PM
On Sat, 13 May 2006 15:20:11 +0100, itsme213 <itsme213@hotmail.com> wrote:
> Where/how do I define a method to construct a new object without calling
> initialize?
>
> e.g. I want
>
> class Foo
> def special_init *args
> end
> end
>
> Foo.construct my_args
> # get a new Foo without calling #initialize
> # calls special_init instead of #initialize
>
This may or may not be a good idea (I'll stay on the fence :)) but here's
a way to do it:
class Foo
class << self
def construct(*args, &blk)
o = allocate
o.special_init(*args,&blk)
o
end
end
def special_init(*args)
@test = args
end
end
p Foo.construct([1,2,3])
# => #<Foo:0xb7f859f8 @test=[[1, 2, 3]]>
--
Ross Bamford - rosco@roscopeco.remove.co.uk