【问题标题】:Rails: Overriding const_missing within a moduleRails:在模块中覆盖 const_missing
【发布时间】:2010-07-19 16:47:49
【问题描述】:

在我的 Rails 应用程序中,我有一个这样定义的模块:

module WhateverModule
  class WhateverClass
    ...
  end
end

此文件 (whatever_class.rb) 位于 /app/models/whatever_module 下

const_missing 正在被 Rails 覆盖,尽管我做了一些解决方法,包括初始化器,但我希望我能以更好的方式实现它。

我的目标是通过自定义 const_missing 方法来解析 WhatModule::Foo(Foo 未定义)。

任何帮助将不胜感激。提前致谢!!

【问题讨论】:

    标签: ruby-on-rails ruby metaprogramming


    【解决方案1】:

    以下似乎在 Rails 2.2.2 中对我来说很好

    module WhateverModule
      def self.const_missing(c)
         # handle missing constant
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多