I wrote the code below :

class Animal
  attr_reader :number

  def initialize(name, number)
    @name = name
    @number = number
  end

  def self.all_numbers
    ObjectSpace.each_object(self).map(&:number)
  end
end

an1 = Animal.new('Dog', 23)
an2 = Animal.new('Tiger', 18)


Animal.all_numbers
# => [18, 23]

Somewhere I read that `ObjectSpace#each_object` can pick up objects that
are abandoned but not yet garbage-collected. It would be a good idea to
check the validity of that before using it in this context. My question
is - How to do that ?

-- 
Posted via http://www.ruby-forum.com/.