Michael Neumann
11/28/2008 8:51:00 AM
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