Is there a way to get all the objects that are of a certain class in Ruby?
To clarify:
class Pokemon
end
pikatchu = Pokemon.new
charmander = Pokemon
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
#=> #
charmander = Pokemon.new
#=> #
Pokemon.pokees
#=> [#, #]