【问题标题】:Const_missing hook isn't firedconst_missing 钩子没有被触发
【发布时间】:2017-08-15 13:45:43
【问题描述】:

我将 const_missing 方法修补到 Object 和不同的上下文中:

auto_loader.rb

%w{../../lib ../../app}.each do |path|
  expanded_path = File.expand_path(path,__FILE__)
  $LOAD_PATH.unshift(expanded_path) unless $LOAD_PATH.include?(expanded_path)
end
class Object
def self.inherited(base)
    base.extend(Class_Methods)
    super
  end

  def const_missing(const)
    puts "loading(in Object) %s" % const.to_s
    path = const.to_s.split('::').compact.map(&:downcase).join('/') 
    require path
    self.const_get(const)
  end
  class << self
    def const_missing(const)
      puts "loading(in Object class << self) %s" % const.to_s
      path = const.to_s.split('::').compact.map(&:downcase).join('/') 
      require path
      self.const_get(const)
    end
  end
  module Class_Methods
    class << self
      def const_missing(const)
        puts "loading(in Class_Methods class << self) %s" % const.to_s
        path = const.to_s.split('::').compact.map(&:downcase).join('/') 
        require path
        self.const_get(const)
      end
    end



    def const_missing(const)
      puts "loading(in Class_Methods) %s" % const.to_s
      path = const.to_s.split('::').compact.map(&:downcase).join('/') 
      require path
      self.const_get(const)
    end

  end

  extend Class_Methods
end

当我运行代码时,我看到:

NameError: Uninitialized constant CONST

没有显示任何puts

我还加载代码:

pry -r ./lib/auto_loader.rb

结果是一样的。

另一方面,如果我手动触发const_missing 方法,我会看到loading(in Object) const

为什么const_missing钩子没有被触发?

【问题讨论】:

    标签: ruby constants


    【解决方案1】:

    您的示例不是最小的并且无法运行。如果我尝试以下操作,它可以正常工作。我认为您的问题与其他问题有关。

    class Object
    
      def self.inherited(base)
        base.extend(Class_Methods)
        super
      end
    
      def self.const_missing_impl(const, location)
        puts "loading(in #{location}) %s" % const.to_s
        path = const.to_s.split('::').compact.map(&:downcase).join('/') 
        puts "require #{path}"
      end
    
      def const_missing(const)
        Object.const_missing_impl(const, 'Object')
      end
    
      class << self
        def const_missing(const)
          Object.const_missing_impl(const, 'Object class << self')
        end
      end
    
      module Class_Methods
        def const_missing(const)
          Object.const_missing_impl(const, 'Class_Methods')
        end
    
        class << self
          def const_missing(const)
            Object.const_missing_impl(const, 'Class_Methods class << self')
          end
        end
      end
    
      extend Class_Methods
    end
    
    a = CONST1
    b = a::CONST2
    c = String::CONST3
    d = Class_Methods::CONST4
    
    # Prints
    loading(in Object class << self) CONST1
    require const1
    loading(in Object class << self) CONST2
    require const2
    loading(in Object class << self) CONST3
    require const3
    loading(in Class_Methods class << self) CONST4
    require const4
    

    【讨论】:

      猜你喜欢
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 2012-06-03
      相关资源
      最近更新 更多