1
我的錢修補const_missing
方法爲Object
和差異背景:Const_missing勾手不解僱
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
鉤子沒有解僱?