On Tue, Jan 28, 2014 at 12:58 AM, Stu P. D'naim <lists / ruby-forum.com> wrote:
>> Robert Klemme wrote in post #1134563:
>> You just need to use an instance variable of the class.
>
> Thanks !
> I'll have to read more about class instance variables

Essentially they are the same as object instance variables, it's just
that the object they belong to happens to be a class. Remember that
classes are objects. You just need to be aware of what object is self
at any point and everything just falls into place.

>> Dave Aronson wrote in post #1134564:
>>
>> Off the top of my head... maybe have it be a hash, keyed by the
>> classes, with the values being the arrays you want?  Or you could find
>> some different approach to whatever the actual problem is you're
>> trying to solve.
>>
>
> I have no idea how to append array assigned as value to a hash, my best
> guess was something like:
> @@hash_of_instances[self.class] = ([] << self) ... which of course
> doesn't work ...

The best way is to have a default proc for the hash that creates the
array and assigns it to missing keys, then you just append:

hash = Hash.new {|h,k| h[k] = []}

hash[self.class] << self

> anyway, from what I googled, seems like its better to avoid usage of
> class variables, seems like in Ruby they are something like global
> variables with limited scope :(

Indeed, and for your use case go with instance variables as Robert has
suggested.

Jesus.