From: Mark Volkmann Date: 2005-08-26T01:04:41+09:00 Subject: Re: determining the attribute names of an object On 8/25/05, David A. Black wrote: > Hi -- > > On Thu, 25 Aug 2005, Hal Fulton wrote: > > > David A. Black wrote: > >> Hi -- > >> > >> On Thu, 25 Aug 2005, Hal Fulton wrote: > >> > >>> > >>> Well, Structs are not implemented by using instance variables (well, > >>> they are, but not the way you'd expect). > >>> > >>> If I were doing it, I would implement Structs so that there was a > >>> one-to-one correspondence between members and instance variables. > >>> Then this wouldn't be an issue. > >> > >> > >> What if you wanted to add methods to the class, or one or more > >> instances, that used instance variables in a non-member/attr-like way? > >> It seems arbitrary to decide that Structs can't or shouldn't do that. > >> > > > > Granted, but that's the opposite problem from the one I > > have fought. If I understand you. > > > > I didn't really mean one-to-one necessarily. > > > > But what I have wished for is the ability to add methods that > > referenced the members as normal instance vars (or attributes). > > > > For example, if you have a member named "alpha" you access it > > from the outside just like any accessor. That leads you (me) > > to believe that it corresponds to an instance var @alpha (as if > > we had said attr_accessor :alpha). But it doesn't. > > > > Some people (read: me) have incorrectly assumed that Struct is > > "shorthand squared" for defining a class, an initialize, and > > a bunch of accessors. But the behavior is different. > > > > Clear? Yeah, I know, there is still some point on which you > > disagree. ;) > > I'm just not sure where you would experience a need for that. What > would that enable you to do that you can't do now? I'm thinking you > could always use self.member = x instead of @member = x, in other > methods, which is arguably a good idea anyway. > > Or are there other differences I'm not taking into account (between > members and attr_*-style attributes)? Okay, I'll revise my statement. ;-) If I create a Struct, I can easily add any methods I want to it because it has generated a new class for me. However, it doesn't seem that I can do the same with OpenStruct. The class of an object created with OpenStruct is OpenStruct, not some newly generated class. I wish OpenStruct were exactly like Struct with the exception of allowing "attributes" to be added dynamically. -- R. Mark Volkmann Partner, Object Computing, Inc.