From: "David A. Black" Date: 2009-09-22T21:36:44+09:00 Subject: Re: start_with Hi -- On Tue, 22 Sep 2009, Rong wrote: > I tried changing the array to an instance variable but the code won't > work. You'd have to make it an instance variable of the class itself, like this: class Person @people = [] def self.people @people end def initialize self.class.people << self end end You were very considerate in using a tweet instead of this mailing list to point out that, in spite of my saying I had reservations about using a constant the way your code did, I had done exactly the same thing on page 108 of my book :-) So let me try to return the kindness by explaining my reservations. The main issue at least to be aware of is what happens when you inherit: class Person P = [] def initialize P << self end end class Rubyist < Person end r = Rubyist.new p Person::P # => [#] In other words, you end up using the same constant for all the subclasses, and thus getting what you might consider false positives (depending on exactly what you want to count). This is also why using a class variable is a problem in this kind of role: it will also be the same variable down the class hierarchy. The other issue is just the question of using a "constant" for something that changes, like this array. I don't consider that to be automatically bad (and it's very different from re-assigning to a constant, which is *really* non-constant), but it feels perhaps like a looser fit than using an instance variable, as in my first example above. The instance variable is designed to maintain state on a per-object (in this case, per class-object) basis, which a constant can do but which isn't their main or most "natural" role. David -- David A. Black, Director Ruby Power and Light, LLC (http://www.rubypal.com) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2)