From: Gavin Kistner Date: 2005-08-25T23:56:03+09:00 Subject: Re: determining the attribute names of an object On Aug 25, 2005, at 8:45 AM, David A. Black wrote: >> 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. > > 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. I think you're correct; I think the problem is simply one of consistency. I expected #puts and #print to output their code inline in an ERB template, but (until I hacked the source) they didn't. There were at least two other ways to achieve the same effect, but it causes a context switch when I'm merrily coding along and have to say "Oh, shit, I forgot, I'm inside __this__ case...I need to write my code a little differently." I feel the same way with Struct...it would be convenient to be able to write: Person = Struct.new( :first, :last ) class Person def fullname "#@first #@last" end end because that's what I (and many others) expected the implementation detail of Struct-created classes to be. Certainly, it's just as easy to write: Person = Struct.new( :first, :last ) class Person def fullname "#{first} #{last}" end end And as you say, it is arguably a good idea to use internal methods of the class to minimize coupling between methods. (Am I correct in assuming that this may result in a non-trivial performance hit, however, invoking methods versus using instance-variable lookup?) It's just a 'special case' from what appears to be the mental model of many people.