これは良い習慣ですか?定数によってアクセスされるオブジェクトを格納します。処理のために他のクラスがアクセスするオブジェクトを格納する方法はたくさんあります。コレクションを指すハッシュ オブジェクトを格納する手段として代わりに定数を使用することは、* @@class_variables 以外に最近やり始めたことです。もしあれば、これに対する賛否両論は何でしょうか?OOP は特に目新しいものではありませんが、悪い習慣となる洞察をいただければ幸いです。
#!/usr/bin/env ruby
OUTSIDE_STATE = true
NAMES = {:objects => []}
module CheckSelf
module ClassMethods
def self.inherited(child)
::NAMES[:objects] << child
end
def select_and_make_calls
_selected = NAMES[:objects].select {|child|
child.purpose()
}.each {|child|
# _child = child.new(child)
child.new(child).call_out
}
end
end
module InstanceMethods
def call_out
puts "FIRING OFF THE CALLS #{self.class}"
end
end
def self.included(receiver)
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
class Parent
def initialize(name)
@name = name
end
include CheckSelf
# The inherited class must be defined in the *
# Body of the class.
def self.inherited(child)
NAMES[:objects] << child
end
end
class ObjectOne < Parent
def self.purpose
OUTSIDE_STATE
end
end
class ObjectTwo < Parent
def self.purpose
true
end
end
Parent.select_and_make_calls