2017-08-15 1 views
1

ich Geld gepatcht const_missing Methoden in Object und in Differenz Kontexten gefeuert:Const_missing Haken ist nicht

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 

Wenn ich den Code ausführen, ich sehe:

NameError: Uninitialized constant CONST 

ohne eines der angezeigten puts.

Ich lade auch den Code mit:

pry -r ./lib/auto_loader.rb 

Das Ergebnis ist das gleiche.

Auf der anderen Seite, wenn ich die const_missing Methode manuell auslösen, sehe ich loading(in Object) const.

Warum ist const_missing Haken nicht ausgelöst?

Antwort

0

Ihr Beispiel ist nicht minimal und läuft nicht. Wenn ich folgendes versuche, funktioniert es ohne Probleme. Ich denke, dein Problem hängt mit etwas anderem zusammen.

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 
Verwandte Themen