现在我声明了类级变量
MyConsumer << Bunny::Consumer
def initialize( [skip] )
@@consumers ||= []
[skip]
at_exit{ self.unregister_and_close }
@@consumers << self
[skip]
end
end
但是,也许在Bunny lib中有一个消费者列表?到目前为止没有找到任何东西
MyConsumer << Bunny::Consumer
def initialize( * )
@@consumers ||= []
# other operations
at_exit{ self.unregister_and_close }
@@consumers << self
# other operations
end
def self.list
@@consumers ||= []
@@consumers.each do |consumer|
puts consumer
end
end
end
MyConsumer.new
=>#<MyConsumer:0x0000001031CA58>
MyConsumer.new
=>#<MyConsumer:0x0000000b896510>
MyConsumer.new
=>#<MyConsumer:0x00000001041a38>
MyConsumer.list
#<MyConsumer:0x0000001031CA58>
#<MyConsumer:0x0000000b896510>
#<MyConsumer:0x00000001041a38>
=> [#<MyConsumer:0x0000001031CA58>,
#<MyConsumer:0x0000000b896510>,
#<MyConsumer:0x00000001041a38>]