From: Austin Ziegler Date: 2004-12-09T04:45:35+09:00 Subject: Re: Freezing Variable Assignment On Thu, 9 Dec 2004 04:22:30 +0900, itsme213 wrote: > "Austin Ziegler" >>> Though it's hard to say "object x has states a, b" when you mean >>> "object x has instance variables a, b". Or "object x has states >>> 1, 2, 3" when you mean "object x has indexed variables >>> (indices?) 1, 2, 3". >> No, it isn't. It is very easy to say that "object x has a state >> of instance variables @a and @b" or "object x has a state of >> indexes 1, 2, and 3". > To each his own, since "easy" and "hard" are subjective :-) To me: > > "object x has slots @a, @b, 1, 2...5" (assuming "ivars" are > unacceptable in Ruby parlance) > is better than > "object x has states @a, @b, 1, 2...5" > which is better, simpler, and more uniform than > "object x has a state of instance variables @a, @b, or a state > of indices 1, 2..5" > > And I'd prefer (e.g. for generic object graph traversal) > Object#each_slot or Object#each_generalized_ivar > over Object#each_state > over Object#each_instance_variable, Array#each_index, Hash#each, > #each_key, #each_value > over Object#each_whatever_an_extension_might_do_in_the_future Yuck. What you're describing isn't Ruby, and tries to combine things for some ivory tower intellectual wankery as opposed to the pragmatism that makes Ruby the language most of us have come to love. There is no reason to iterate over an object's instance variables as a standard feature of the language. It is a conceptual *mistake* to consider instance variables equivalent to the contents of a Hash or Array. If you absolutely *must* iterate over instance variables in a generic object, then use #instance_variables combined with #instance_variable_[gs]et. An object has state. Whether that state is comprised of multiple instance variables, other internal state (e.g., Hash or Array collections), or both is ultimately irrelevant. If I do the following: require 'transaction/simple' a = %w(a b c d e f g) a.extend(Transaction::Simple) a.start_transaction The object referred to by 'a' has both internal state (the array values, e.g., self[0..-1]) and several instance variables added by Transaction::Simple. What is the state of the object? Both the internal state and the instance variables. >> Frankly, I don't think that a common term is useful or necessary. >> Array and Hash objects are fundamentals; everything else uses >> instance variables. > String.superclass #=> Object > String.instance_variables #=> [] > I would find it hard to explain the above using your Hash, Array, > and instance variable. So is there some hidden instance variable > ("slot") at work here? What is special about it? Why is it hidden? > Because it is implemented in C? Like Array and Hash? But we'd then > be making implementation distinctions which I was trying to > abstract away. And I'm telling you that what you're doing is a fruitless exercise for some ideal of purity that *is not Ruby*. A String has state. It may *also* have instance variables (see Transaction::Simple) that contribute to its state. But whether implemented in Ruby, C, or assembly, it seems highly unlikely that anything will ever be done to expose the internal state implementation of String, when String is a fundamental object. What would the instance variable of a Fixnum be? IMO, that's a silly question -- it is a value. > Most descriptions of an object model choose some term like "slot" > or "instance variable" or something else. I think this would be an > improvement on Ruby's (decent) description of its object model. And I think that it would be a waste of time, effort, and clarity. This would do nothing more than muddle Ruby's object model for people who aren't you. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca