From: Rick DeNatale Date: 2007-05-12T11:46:52+09:00 Subject: Re: instance_variables doesn't return unassigned variables 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. 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. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/