0

Redefinition of const_missing method in the Singleton class of Module class doesn't seem to work. But it works if I redefine directly in the class Module. Any reason why?

class Module
  class << self
    def const_missing(constant)
      puts "This doesn't work!"
    end
  end
end

Hello

Where as the following works!

class Module
  def const_missing(constant)
    puts 'This works!'
  end
end

Hello

Context:

  • Trying to use super in cases where the constants fall into a category thats not to be overridden. Say constants that do not match a pattern should still cause a NameError.
Jikku Jose
  • 18,306
  • 11
  • 41
  • 61

1 Answers1

1

Why do you think defining const_missing on Module’s eigenclass is not working? It perfectly works:

▶ class Module
▷   class << self  
▷     def const_missing(constant)    
▷       puts "This doesn't work!"      
▷     end      
▷   end    
▷ end  
#⇒ :const_missing
▶ Module::F
#⇒ This doesn't work!

The question is what do you want to achieve? Whether you are interested in handling cases when your Module/Class is invoked to call it’s constant like:

module M ; end
puts M::MissingConst

You are expected to implement const_missing on M’s eigenclass. Which singleton’s superclass is apparently Module class itself, not Module’s eigenclass (M.singleton_class.superclass => Module.)

Whether you want to handle virtually all constants referenced by titlecased name without namespace, you might use:

class Object
  class << self
    def const_missing(name)
      puts 'bingo '
    end
  end
end

▶ F
#⇒ bingo 
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • I am trying to intercept `const_missing` method on *any constant*, including thats not namespaced by `Module`. Try `F` without `Module::` in its front? – Jikku Jose Jan 19 '15 at 16:47
  • Then I would suggest you to google for 'ruby main context' (e.g.: http://stackoverflow.com/a/917842/2035262), since ruby will try to resolve `F` in all effective namespaces until it reaches `main` [pseudo]object. You probably want to define method missing on this main object’s eigenclass. – Aleksei Matiushkin Jan 19 '15 at 16:51
  • Yes, exactly! I am trying to override the Module thats at the deepest level so that any uninitialized constant can be dealt in a particular way. – Jikku Jose Jan 19 '15 at 17:04
  • Oh cool! `Object` is the one who handles `constants`? Was assuming I was already defining at the fundamental layer. – Jikku Jose Jan 19 '15 at 18:58
  • 1
    Just a detail: `M.singleton_class.superclass => Module`. – Cary Swoveland Jan 19 '15 at 22:12
  • Didn't think `singleton_class` have any `superclass`! Thanks! I am yet to get a better grasp of the Ruby Object Model. – Jikku Jose Jan 20 '15 at 03:17