0
我正在使用method_missing
爲詞彙表中的名稱間距常量定義類。爲了有效,我需要從BasicObject
繼承詞彙類,否則沒有標準的對象方法可用作詞彙表術語(因爲方法不會丟失:)。但是,當我從BasicObject
繼承時,我發現我無法在另一個模塊中調用實用程序方法。下面的代碼說明了壓縮形式的問題:基於BasicObject的Ruby類無法訪問其他模塊中的代碼
module Foo
class Bar
def self.fubar(s)
"#{s} has been fubar'd"
end
end
end
class V1
def self.method_missing(name)
Foo::Bar.fubar("#{name} in v1")
end
end
class V2 < BasicObject
def self.method_missing(name)
Foo::Bar.fubar("#{name} in v2")
end
end
# this works
puts V1.xyz
# => xyz in v1 has been fubar'd
# this doesn't
puts V2.xyz
# => NameError: uninitialized constant V2::Foo
我將需要添加到V2
所以,當我嘗試打電話給助手模塊也不會產生一個未初始化常數錯誤?