From: Dave Thomas Date: 2004-11-12T10:53:33+09:00 Subject: Re: ruby idiom for attribute definition? On Nov 11, 2004, at 19:43, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: ruby idiom for attribute definition?" > on Fri, 12 Nov 2004 04:06:39 +0900, Dave Thomas > writes: > > |That's why I said you have to use the accessor methods. Structs cheat. > > I don't consider it cheating. Accessing instance variables is sort of > cheating (that's part of the reason for ugly "@"). Except in structs you still have the instance variables (the state has to be somewhere). You just can't access them. That's what I meant by cheating... :) > |Matz: we discussed this before, but it would be really nice if Struct > |set up the IVs too: that way this idiom would be more generally > useful. > > Hmm, instance variables are internal information. If you don't know > the internal, you should use accessors (self.a this case), even in the > methods. I'm not excited for Structs to have their members in > instance variables. Agreed in general (in fact I argued that a couple of posts back). But the idiom class A < Struct(:x, :y) end could be really useful for classes that want IVs initialized from the constructor, and which provide accessors for them by default, that I wonder if we could bend the rules :) I tend to use the construct for Structs where I want to add just one or two helper methods. Cheers Dave