因此,在一個Ruby類,你可以用一個成語,如class << self
類似如下:紅寶石:打開模塊的單
class SalesOrganization
def self.find_new_leads
...
end
class << self
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
add_transaction_tracer :find_new_leads, :category => :task
end
end
我的問題是,如果SalesOrganization
是實際上是一個Module
,而不是Class
什麼。這是做我希望它會做什麼,或者我打開了一些我不該涉及的黑魔法?
# Potentially terrible code
module SalesOrganization
def self.find_new_leads
...
end
class << self
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
add_transaction_tracer :find_new_leads, :category => :task
end
end
如何訪問模塊單例類?
這實際上並不回答關於如何打開模塊的單例類的原始問題。我不包括該模塊的任何地方,包括的方法將永遠不會觸發。我現在只是將它用作命名空間。 – randombits