33

Ruby 中有没有办法让一个类知道它存在多少个实例并且可以列出它们?

这是一个示例类:

class Project

  attr_accessor :name, :tasks

  def initialize(options)
    @name = options[:name]
    @tasks = options[:tasks]
  end

  def self.all
    # return listing of project objects
  end

    def self.count
          # return a count of existing projects
    end


end

现在我创建这个类的项目对象:

options1 = {
  name: 'Building house',
  priority: 2,
  tasks: []
}

options2 = {
  name: 'Getting a loan from the Bank',
  priority: 3,
  tasks: []
}

@project1 = Project.new(options1)
@project2 = Project.new(options2)

我想要的是有类方法Project.all,并Project.count返回当前项目的列表和计数。

我该怎么做呢?

4

4 回答 4

50

您可以使用ObjectSpace模块来执行此操作,特别是each_object方法。

ObjectSpace.each_object(Project).count

为了完整起见,以下是您在课堂上使用它的方式(向sawa 致敬)

class Project
  # ...

  def self.all
    ObjectSpace.each_object(self).to_a
  end

  def self.count
    all.count
  end
end
于 2013-01-14T12:50:06.103 回答
7

一种方法是在创建新实例时跟踪它。

class Project

    @@count = 0
    @@instances = []

    def initialize(options)
           @@count += 1
           @@instances << self
    end

    def self.all
        @@instances.inspect
    end

    def self.count
        @@count
    end

end

如果你想使用ObjectSpace,那么它

def self.count
    ObjectSpace.each_object(self).count
end

def self.all
    ObjectSpace.each_object(self).to_a
end
于 2013-01-14T12:24:27.687 回答
4
class Project
    def self.all; ObjectSpace.each_object(self).to_a end
    def self.count; all.length end
end
于 2013-01-14T12:50:56.757 回答
3

也许这会起作用:

class Project
  class << self; attr_accessor :instances; end

  attr_accessor :name, :tasks

  def initialize(options)
    @name = options[:name]
    @tasks = options[:tasks]

    self.class.instances ||= Array.new
    self.class.instances << self
  end

  def self.all
    # return listing of project objects
    instances ? instances.dup : []
  end

  def self.count
    # return a count of existing projects
    instances ? instances.count : 0 
  end

  def destroy
    self.class.instances.delete(self)
  end
end

但是您将不得不手动销毁这些对象。也许可以基于 ObjectSpace 模块构建其他解决方案。

于 2013-01-14T12:29:02.570 回答