From: Robert Dober Date: 2007-05-13T17:44:32+09:00 Subject: Re: instance_variables doesn't return unassigned variables On 5/12/07, Rick DeNatale wrote: > On 5/11/07, Robert Dober wrote: > > > > > The idea is to have attributes and not instance variables, creating > > instance variables on the fly with the attr method will not > > distinguish them from other instance variables. > > > > I reckon that the only sense I could make from the post was when OP > > talked about the instance variables """created""" by the existence of > > accessors. > > You do that for him, I would think it is worth to distinguish them. > > Also have a look at inheritance, which is not handled by your code. > > I'm not sure what the OP was really looking for. > > My sense was that there wasn't a real requirement but a confusion over > why instance variables aren't created when you 'declare' an accessor. > > Since the OP doesn't seem to have posted to this thread since the OP, > I don't know how much he really cares. > > One of the interesting aspects of Ruby as compared to many other OO > languages such as Java, C++ and Smalltalk, is that instance variables > aren't actually declared, and are really dynamc properties of the > instances themselves rather than all instances of the class. So true and that is why I thought that having #instance_variables and having my little #attributes has nothing to do with each other and that indeed it might be a good idea to have an introspection tool concerning the declared attributes of a *Class*. I do not even touch instances or instancevariables in my implementation but I provide a class based attributes introspection method that respects the inheritance feature of attr_accessor (could easily be extended for the other attr_* methods of course). I do not really care if this is what OP wanted, though I thought that was likely what he or somebody else wanted, I get easily confused, you know. Most important I felt that by trying to understand what seemed like a nonsense request gave me the oppurtunity to do something useful. Assuming (as dangerous as that might be) that this was what OP or someone else had in mind. Sorry if I am talking too many liberties. > > In the other mentioned languages, declaring instance variables creates > a template which is used to map the storage taken up by an object, > instance variable x is at offset y from the beginning of the object, > etc. > > In Ruby instance variables are actually values in a hash which the > instance owns, and are placed there only when code executes which sets > the instance var. > > In this way, Ruby is more like Self than those other languages. Self > objects are really just a hash of slot names to slot values. Dave > Ungar used to taunt Smalltalk implementors by asking them to add an > instance variable to the Object class, which usually crashed the > system. > > Self went a little farther than Ruby in this regard since it unified > 'instance' variables and methods, methods are just slots whose values > are executable. In the same regard, all slot values are 'inheritable' > in the sense that objects can delegate to other objects when searching > for a named slot which is missing, which is how Self gets the effect > of classes, mixins, etc. Anyway if there was any confusion about the concept you have shed quite some light on it. > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ > > Cheers Robert -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw