オブジェクトのすべてのインスタンスをグローバル変数に保存する必要があるため、別のオブジェクトからそのインスタンスにアクセスできます。パラメタのようにそれらを渡す必要はありません。グローバル変数にインスタンスを保存
私の解決方法では、インスタンスを変数に入れるメソッドを持つmixinがあります。また、オープンクラスのテクニックを使用してObject
にそのmixinを含めるので、他のオブジェクトもそのメソッドを使用します。
class Object
include Favourite
end
module Favourite
def favourite_it
#if the variable its not initialized:
@favourites.class == Array.class ? @favourites.push(self) :
@favourites = [].push(self)
end
def get_favourites
@favourites
end
end
#this class is only an example
class Dog
def initialize age
@age = age
end
end
class Dog_T
#all instances of this class will be saved in the variable
def initialize age
@age = age
favourite_it
end
end
class Handler
def do_something
#here I need to access the variable with all the instances of favourites to do something to them
end
end
そして、ここで簡単なテスト
handler = Handler.new
d1 = Dog_T.new(10)
d2 = Dog_T.new(12)
all_f = Handler.get_favourites
expect(all_f[0].age).to eq 10
expect(all_f[1].age).to eq 12
d3 = Dog_T.new(15)
all_f = Handler.get_favourites
expect(all_f[3].age).to eq 15
です(私はまだ、グローバル変数を使用していないので、それが理にかなって)私はこれを実行しようとしましたが、唯一の各インスタンスは、別のリストに自分自身を救います。
リストを1つだけ作成し、作成時にインスタンスを追加し、そのリストを空にして操作できるようにするにはどうすればHandler
か。