クラスが存在するインスタンスの数を知るための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
カウントを返すことです。
どうすればよいですか?