How to find each instance of a class in Ruby

Yes, one could use ObjectSpace, but in practice one would simply keep track of instances as they are created.

class Pokemon
  @pokees = []
  self.class.public_send(:attr_reader, :pokees)

  def initialize
    self.class.pokees << self
  end
end

pikatchu = Pokemon.new
  #=> #<Pokemon:0x00005c46da66d640> 
charmander = Pokemon.new
  #=> #<Pokemon:0x00005c46da4cc7f0> 
Pokemon.pokees
  #=> [#<Pokemon:0x00005c46da66d640>, #<Pokemon:0x00005c46da4cc7f0>] 

The solution is to use ObjectSpace.each_object method like

ObjectSpace.each_object(Pokemon) {|x| p x}

which produces

<Pokemon:0x0000010098aa70>
<Pokemon:0x00000100992158>
 => 2 

Details are discussed in the PickAxe book Chapter 25


ObjectSpace is a simple solution, but note that you can only add to it; removing objects requires garbage collection and that can get messy.

Here's an example of tracking class members that allows you to clear the count. This could be useful, for example, when writing a dummy class for specs.

class Foo
  @@instances = []

  def initialize
    @@instances << self
  end

  def self.clear
    @@instances.clear
  end

  def self.all
    @@instances
  end
end

Usage:

(1..10).each { Foo.new }
Foo.all.size # 10
Foo.clear
Foo.all.size # 0