[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: private #initialize

Yukihiro Matsumoto

11/27/2008 11:33:00 PM

Hi,

In message "Re: private #initialize"
on Fri, 28 Nov 2008 05:38:47 +0900, Yossef Mendelssohn <ymendel@pobox.com> writes:

|And it seems odd to have
|something this central to the language skirting the boundaries of
|access control. Is it simply because an object shouldn't be re-
|initialized once it's created? And if so, is that so horrible?

#initialize is, by its design, supposed to be called only from within
#new to separate per object/class initialization from the #new, thus
you don't have to redefine #new. When you need/want to redefine #new,
it's a sign of a bad design, I believe.

One of the reason #initialize being private is to tell you bad design.

matz.

3 Answers

Robert Dober

11/28/2008 1:24:00 AM

0

On Fri, Nov 28, 2008 at 12:32 AM, Yukihiro Matsumoto <matz@ruby-lang.org> w=
rote:

> #initialize is, by its design, supposed to be called only from within
> #new to separate per object/class initialization from the #new, thus
> you don't have to redefine #new. When you need/want to redefine #new,
> it's a sign of a bad design, I believe.
I am delivering this code to your judgment

module Immutable
def new *args, &blk
o =3D allocate
o.send( :initialize, *args, &blk )
o.freeze
end
end

>
> One of the reason #initialize being private is to tell you bad design.

I can not come up with a better design as the above. But I am
listening of course.
Calling freeze on self at the end of #initialize is really asking for
trouble with subclasses.

Now that all said, it is good that #initialize is private because it
makes you think before doing tricky things as above
but I would not call it bad design.

Cheers
Robert

--=20
Ne baisse jamais la t=EAte, tu ne verrais plus les =E9toiles.

Robert Dober ;)

Michael Neumann

11/28/2008 8:51:00 AM

0

Robert Dober schrieb:
> On Fri, Nov 28, 2008 at 12:32 AM, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
>
>> #initialize is, by its design, supposed to be called only from within
>> #new to separate per object/class initialization from the #new, thus
>> you don't have to redefine #new. When you need/want to redefine #new,
>> it's a sign of a bad design, I believe.
> I am delivering this code to your judgment
>
> module Immutable
> def new *args, &blk
> o = allocate
> o.send( :initialize, *args, &blk )
> o.freeze
> end
> end

How about this?

module Immutable
def new(*args, &blk)
super.freeze
end
end

Okay, this will not work if you redefine #new yourself. Or that?

module Immutable
def self.included(klass)
class << klass
alias __old_new new
def new(*args, &blk)
__old_new(*args, &blk).freeze
end
end
end
end

(will not work when including into a module)

Regards,

Michael


Stefan Rusterholz

11/28/2008 12:12:00 PM

0

Michael Neumann wrote:
> How about this?
> module Immutable
> def self.included(klass)
> class << klass
> alias __old_new new
> def new(*args, &blk)
> __old_new(*args, &blk).freeze
> end
> end
> end
> end

Wrong hook. new is a class method, so you'll want to extend, so def
self.extended.

Regards
Stefan Rusterholz
--
Posted via http://www.ruby-....