From: "trans. (T. Onoma)" Date: 2005-01-17T23:22:30+09:00 Subject: Re: Fwd: [suby-ruby] Your all time desired fundemental Ruby mod On Monday 17 January 2005 08:33 am, David A. Black wrote: | Hi -- | | On Mon, 17 Jan 2005, trans. (T. Onoma) wrote: | > On Monday 17 January 2005 02:51 am, Martin DeMello wrote: | > | David A. Black wrote: | > | > You're right -- I meant to clarify that I'm talking about 2.0 class | > | > vars, which as I understand it will be truly per-class and will | > | > differ from instance variables of Class objects principally in that | > | > they will be in scope in instance methods. I would rather not have a | > | > separate construct that overlaps so much with instance variables. | > | | > | I can think of at least one good us for the current @@ variables - | > | subclassable GUI components that inherit hierarchy-shared look-and-feel | > | properties from the parent class. What would be the elegant way to do | > | this if @@ variables go per-class? | > | > You would have to do it via class instance vars. | | They're per-class too :-) You'd need accessor methods, I think. Yes, of course: "via class instance thru via accessors" is waht I should have said. | > The problem with these IMHO | > is still the intialization issue, as with modules (see ruby-talk:121611, | > http://urltron.com/nd) Matz, do you think we can get something in the way | > of remedying that? For me it's one of the biggest sores in my code. | | I'm not sure I get what the problem is here. In your examples you're | overriding #initialize, and calling super to invoke the old one. | That's pretty standard procedure -- it's what super is basically for, | and it seems like the simplest design. (I'm not sure what's supposed | to happen in the future if instance vars become private to their | module.) Yes, true. But good SOC/ecapsulation meants that I should be able to modify a class with a module without having to adjust pre-existing parts of the class. For instance lets say I have class: class C def initialize @y = 11 end end I want to expand the functionality of this class with a module I have: module M def x ; @x ; end end But @x needs to default to an array. So what do I do? Presently I have two choices. Either change M to: module M def x ; @x ||= [] ; end end or use super as you suggest. But I don;t want to just recopy C for what if it changed internally later? So, module M def initialize @x = [] end def x ; @x ; end end class C alias_method :init, :initialize def initialize(*args, &blk) super init end end None of this is need though if we just had some way to "initialize_always". Does that explain better? T.